How to Initialize const static object data members in a header file?

S

Seb

I am trying to initialize a const static object data member in a header
file? The following code errs.

class Object
{
public:
virtual const char* ToString() { return "Object"; }
virtual DataType GetType() { return DataType( "Object" ); }
protected:
const static DataType _dataType( "Object");
};

I don't want to have to create a .cpp file for many of my utility headers.
Any help would be greatly appreciated.

Thanks
 
A

Alf P. Steinbach

* "Seb said:
I am trying to initialize a const static object data member in a header
file? The following code errs.

class Object
{
public:
virtual const char* ToString() { return "Object"; }
virtual DataType GetType() { return DataType( "Object" ); }
protected:
const static DataType _dataType( "Object");
};

I don't want to have to create a .cpp file for many of my utility headers.
Any help would be greatly appreciated.

Method 1:

class Gurgle
{
public:
static DataType const moo()
{
static DataType const theDataType( "Gurgle" );
return theDataType;
}
};

Method 2:

template< class ADummy_ >
class Gurgle_
{
public:
static DataType const moo;
};

template< class ADummy_ >
DataType const Gurgle_::moo( "Gurgle" );

struct Dummy_ {};
typedef Gurgle_<Dummy_> Gurgle;
 
S

Seb

Alf P. Steinbach said:
Method 1:

class Gurgle
{
public:
static DataType const moo()
{
static DataType const theDataType( "Gurgle" );
return theDataType;
}
};

Method 2:

template< class ADummy_ >
class Gurgle_
{
public:
static DataType const moo;
};

template< class ADummy_ >
DataType const Gurgle_::moo( "Gurgle" );

struct Dummy_ {};
typedef Gurgle_<Dummy_> Gurgle;

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Thanks Alf... that takes care of my dillema.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top