Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C++ Static Const Initialization

Posted on 1998-05-28
10
Medium Priority
?
914 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 800 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

971 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