469,951 Members | 2,744 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

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

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
Jul 22 '05 #1
2 3866
* "Seb" <as**********@somewhere.com> schriebt:
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;

--
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?
Jul 22 '05 #2
Seb
"Alf P. Steinbach" <al***@start.no> wrote in message
news:40****************@news.individual.net...
* "Seb" <as**********@somewhere.com> schriebt:
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;

--
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.
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by bob | last post: by
9 posts views Thread by subramanian | last post: by
4 posts views Thread by Bram Kuijper | last post: by
10 posts views Thread by Stephen Howe | last post: by
9 posts views Thread by Steven Woody | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.