472,353 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 3987
* "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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Seb | last post by:
I am trying to initialize a const static object data member in a header file? The following code errs. class Object { public: virtual const...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I...
3
by: Henryk | last post by:
I'm using the gcc with the -pedantic setting. On my embedded system I need a parameter class that contains all setting for the software. This...
4
by: bob | last post by:
Why doesn't c++ let me initialize static members like this: class MySound { static CSoundManager* g_pSoundManager = NULL; };
9
by: subramanian | last post by:
I am a beginner in C++. Suppose I want to build a class: I have given below the starting code: class Date { int day, month, year; static...
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
9
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where...
5
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.