Solved

C++ Static Const Initialization

Posted on 1998-05-28
10
837 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

820 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