Link to home
Start Free TrialLog in
Avatar of barryc
barryc

asked on

C++ Static Const Initialization

In the ANSI C++ standard is it legal to initialize static const class data members in the class declaration (.h file):

class foo {
 . . .
private:
   static const int arr_size=10;  // is this line legal???
   int myArr[arr_size];                // i need the value here
}

I'm having problems compiling the KDE support libraries with Sun's CC due to a line in a class declaraction which initializes a static const member of a template class.

Thanks,
Barry M. Caceres
barryc@idea-center.com
Avatar of nietod
nietod

We (alexo, ronslow and myself) had a discussion about this a few months ago.  I believe it was decided that it is now legal to do so.  But that not all compilers support it.  You can get around this by using enums on a compiler that doesn't support it like

  enum SomeTypeName { int arr_size=10);

Alex will be up in a few hours and proably can give you the definitive answer.
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of barryc

ASKER

This enum thing gets around the problem when the variables are
of type "int", but what about unsigned int, unsigned char, etc...
I can see it making even less sense for constant strings (static
const char*) or constant static members whose type is a user
defined class.

So far I think I have found that it works with g++ and not with
CC.  Which of these compilers is "right"?
If you don't use it as the size of an array, you can initialize it like this:

// .h

class foo {
 . . .
private:
   static const char *pszString;
}

// .cpp

const char *foo::pszString = "OK";


What's the compiling error message with CC?
Opps.  Ignore my attempt with the enum.  Its got 2 typos.  Chensu has it right.

I believe it is now part of the standard to allow statics to be initialized in this way.  
I believe Chensu is actually wrong on this one.  Actually out date would be more accurate.  Take a look at

https://www.experts-exchange.com/topics/comp/lang/cplusplus/Q.10050536

So the problem is that your CC compiler is just not up to the new standard and the g++ one is.  You can get arround this using enum's as we suggested.  
nietod,

>I believe Chensu is actually wrong on this one.

I don't think my answer is wrong. Yes, I didn't mention (and I didn't know):

>NEW compilers that conform to the C++ draft standard will accept:

    // in .H file
    struct X
    {
        static const int PROP = 17;
    };

I think another Expert yonat is likely to be clearer on this issue. He is familiar with the latest C++ standard.
Agreed.  I did correct myself.  I said you were actually "out of date".  Your answer was not really completely right since it is important to know that the code was written to work with the new standard and does not have a syntax error.  Rather the problem lies with the compiler, not the code.

I was told that Yonat was a she not a he.
>I was told that Yonat was a she not a he.

Really?
That's from Alexo, I believe it is Sharon Yonat.