473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 4085
* "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 char* ToString() { return "Object"; } virtual...
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 am seeking to determine whether I am programming...
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 class needs to be ROM-able to locate it in some kind...
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 Date default_date; };
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 static members of a class in the constructor, I...
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 is included in multiple translations units, but 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 should I put these initialization code? I don't want...
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 this class SystemName { public:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.