Console C++ - static const xxyy
Defining static const variables in a class -
g++ allows it, Borland does not.
The following fragment is from the problem set provided by the teacher.
#include
struct xx {
string name;
int number;
};
class zz
{
public:
typedef xx valueType;
private:
static valueType NULL_ITEM = {" ", -1};
};
Borland C++
| Visual C++
| GNU g++ (Unix)
Borland C++ 5.02
I never found a way to create and initialize NULL_ITEM
using the syntax provided by the teacher. Instead, I created
a global variable in the .cxx (.cpp) file and initialized
it in the constructor.
This technique makes the variable both static and private since
it is only known inside the .cxx file. However, it looses some
of its namespace localization since other global valiables
could have the same name.
#include
struct xx {
string name;
int number;
};
class zz
{
public:
typedef xx valueType;
private:
};
// This is in the .cxx file
zz::valueType NULL_ITEM ; // This is private since other files only
// import the header file.
zz::zz ()
{
NULL_ITEM.name = " ";
NULL_ITEM.number = -1;
}
void zz::init ( )
{
for (int i=0;i
Microsoft Visual C++
GNU g++ (Unix)
Author: Robert Clemenzi -
clemenzi@cpcug.org