• Announcements

Archived

This topic is now archived and is closed to further replies.

const char [] troubles with class def's

Recommended Posts

I''m having trouble trying to get my class definitions to work with a const char szAppName[] = "MyApplication". for example: class CApplication { private: // variables HINSTANCE hThisInstance; HWND hWndMain; bool bQuit; const char szAppName[] = "My Application"; }; this causes several weird errors. Any ideas?

Share on other sites
AFAIK, if declaring a class variable constant you MUST define it it outside of the class declaration.

Share on other sites
You need to initialise it in the constructor or something. You can''t initialise in the class declaration.

Share on other sites
You can declare constant members within the declaration but they must be initialize in list form in the constructor.

eg

class MyClass
{
public:
MyClass(void);
~MyClass(void);

private:
const int m_MyInt;
};

MyClass::MyClass(void)
:m_MyInt(3)
{
}

Share on other sites
Thanks Gillies, that worked almost perfectly

How would I declare more than one though?

MyClass::MyClass(void)
:m_MyInt(3), m_MyInt2(4)
{
}

Got it

Share on other sites
If you want to define a constant of integral type (that is, int, double, etc.) inside a class, you can declare it static and initialize directly.

  #include class CCLass{public: static const int ID = 234;}; int main(){ printf("%i", CCLass::ID);}

However this will not work with char[].

Edited by - Advanced Bug on October 22, 2001 5:24:16 AM

Share on other sites
That doesn''t work for ints, either, at least in my compiler:
  class Test{ static const int m_const = 42;};

C:\projects\test\test.h(6) : error C2258: illegal pure syntax, must be ''= 0''
C:\projects\test\test.h(6) : error C2252: ''m_const'' : pure specifier can only be specified for functions

My understanding is that C++ doesn''t allow member initialization in the class definition, period. What version are you using that allows this?

Share on other sites
AFAIK, static members must be intialized at file scope, which means Advanced Bug''s example can''t be right.

Share on other sites
Just wondering, if you have a constant inside a class wouldnt it make it so a new constant is allocated for each

instance of the class? I mean, it would allocate new memory for each constant, and it will be the same constant, so

if you have 1000 objects you will be wasting a lot of memory in duplicate values wouldnt you? why not declare it

outside the class, and have all instances access the same memory? I am not sure if the compiler would know of

something like this to optimise, so I ask.

Share on other sites
In such cases, we use static member variables. Static member variables (note that they aren''t the same thing as static variables!) belong to the class; there is only one copy regardless of how many instances of the class exist. It''s very useful for reference counting, for example. It is precisely because only one copy exists for the entire class that it must be initialized at file scope (outside of the class declaration, but it can be in the same file - after the class declaration).

Static variables are variables allocated when first encountered which do not lose their value and are not deallocated for the duration of the program. This is very useful for certain "reentrant" functions (I remember using them for timeslice''ing under DOS).

Share on other sites
To Stoffel: You're using VC++, am I right? This behavior is a "feature" of the MS implementation, but AFAIK it should be possible in ANSI C++.
Have a look at http://support.microsoft.com/support/kb/articles/Q241/5/69.ASP.

Edited by - VolkerG on October 22, 2001 3:36:02 PM

Share on other sites
Ah, right you are. My bad. Maybe it''ll be fixed in .Net. =)

• Partner Spotlight

• Forum Statistics

• Total Topics
627661
• Total Posts
2978496

• 10
• 12
• 22
• 13
• 33