Archived

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

const char [] troubles with class def's

This topic is 5895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
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 <cstdio>

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites