Solved

C++ Static Const Initialization

Posted on 1998-05-28
10
823 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:barryc
  • 5
  • 4
10 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1164896
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.
0
 
LVL 23

Accepted Solution

by:
chensu earned 200 total points
ID: 1164897
No. You have to initialize it as follows since it is a static member variable.

const int foo::arr_size=10;

But, the compiling error will occur at int myArr[arr_size]; because it expects constant expression.

So, you can do this.

class foo {
 . . .
private:
   enum { ARR_SIZE = 10 };
   int myArr[ARR_SIZE];
}

0
 

Author Comment

by:barryc
ID: 1164898
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"?
0
 
LVL 23

Expert Comment

by:chensu
ID: 1164899
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?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164900
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.  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 1164901
I believe Chensu is actually wrong on this one.  Actually out date would be more accurate.  Take a look at

http://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.  
0
 
LVL 23

Expert Comment

by:chensu
ID: 1164902
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164903
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.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1164904
>I was told that Yonat was a she not a he.

Really?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1164905
That's from Alexo, I believe it is Sharon Yonat.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question