Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing const member within class declaration in C++

Tags:

c++

g++

In PHP and C# the constants can be initialized as they are declared:

class Calendar3 {     const int value1 = 12;     const double value2 = 0.001; } 

I have the following C++ declaration of a functor which is used with another class to compare two math vectors:

struct equal_vec {     bool operator() (const Vector3D& a, const Vector3D& b) const     {         Vector3D dist = b - a;         return ( dist.length2() <= tolerance );     }      static const float tolerance = 0.001; }; 

This code compiled without problems with g++. Now in C++0x mode (-std=c++0x) the g++ compiler outputs an error message:

error: ‘constexpr’ needed for in-class initialization of static data member ‘tolerance’ of non-integral type

I know I can define and initialize this static const member outside of the class definition. Also, a non-static constant data member can be initialized in the initializer list of a constructor.

But is there any way to initialize a constant within class declaration just like it is possible in PHP or C#?

Update

I used static keyword just because it was possible to initialize such constants within the class declaration in g++. I just need a way to initialize a constant in a class declaration no matter if it declared as static or not.

like image 211
ezpresso Avatar asked Feb 04 '12 15:02

ezpresso


People also ask

How do you initialize a const member variable in a class?

To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.

How a const data member can be initialized?

The constant data member is initialized using the const keyword before the data type inside the class. The const data members cannot be assigned the values during its declaration; however, they can assign the constructor values. cout << " The value of constant data member 'A' is: " << obj.

Is it necessary to initialize const variables?

In C++ the const keyword has been improved and you can declare real const values. That's why it has to be initialized before compiling the code. The rule is slightly different: to be used in an integral constant expression, the initialization must be visible to the compiler.

How do you declare a constant in C?

The const keyword Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.


1 Answers

In C++11, non-static data members, static constexpr data members, and static const data members of integral or enumeration type may be initialized in the class declaration. e.g.

struct X {     int i=5;     const float f=3.12f;     static const int j=42;     static constexpr float g=9.5f; }; 

In this case, the i member of all instances of class X is initialized to 5 by the compiler-generated constructor, and the f member is initialized to 3.12. The static const data member j is initialized to 42, and the static constexpr data member g is initialized to 9.5.

Since float and double are not of integral or enumeration type, such members must either be constexpr, or non-static in order for the initializer in the class definition to be permitted.

Prior to C++11, only static const data members of integral or enumeration type could have initializers in the class definition.

like image 59
Anthony Williams Avatar answered Sep 20 '22 18:09

Anthony Williams