By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,575 Members | 3,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,575 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
* "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

P: n/a
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.