Solved

C++ Static Const Initialization

Posted on 1998-05-28
10
819 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>I was told that Yonat was a she not a he.

Really?
0
 
LVL 22

Expert Comment

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now