How to initialize const member variable in class?

#include <iostream> using namespace std; class T1 { const int t = 100; public: T1() { cout << "T1 constructor: " << t << endl; } }; 

When I try to initialize the const member t variable with 100. But this gives me the following error:

 test.cpp:21: error: ISO C++ forbids initialization of member 't' test.cpp:21: error: making 't' static 

How can I initialize the value of const ?

+75
c ++ const
Jan 24 '13 at 6:53
source share
9 answers

The const variable indicates whether the variable is volatile or not. The given constant will be used every time a variable is specified. The assigned value cannot be changed during program execution.

The Bjarne Stroustrup explanation summarizes briefly:

A class is usually declared in the header file, and the header file is usually included in many translation units. However, to avoid complex linker rules, C ++ requires each object to have a unique definition. This rule will be violated if C ++ allows the definition of a class in a class, which should be stored in memory as objects.

A const variable must be declared inside the class, but it cannot be defined in it. We need to define a const variable outside the class.

 T1() : t( 100 ){} 

Here, the assignment of t = 100 occurs in the list of initializers, much before the initialization of the class.

+89
Jan 24 '13 at 7:22
source share

Well, you can do it static :

 static const int t = 100; 

or you can use the element initializer:

 T1() : t(100) { // Other constructor stuff here } 
+42
Jan 24 '13 at 6:56
source share

There are several ways to initialize const elements within a class.

Defining a const element in the general case also requires variable initialization.

1) Inside the class, if you want to initialize const, the syntax is similar to this

 static const int a = 10; //at declaration 

2) The second method may be

 class A { static const int a; //declaration }; const int A::a = 10; //defining the static member outside the class 

3) Well, if you do not want to initialize when declaring, then the other way is through the constructor, the variable must be initialized in the initialization list (not in the body of the constructor). It should be like that.

 class A { const int b; A(int c) : b(c) {} //const member initialized in initialization list }; 
+23
Jun 25 '14 at 3:56
source share
  • You can upgrade your compiler to support C ++ 11, and your code will work just fine.

  • Use the initialization list in the constructor.

     T1() : t( 100 ) { } 
+12
Jan 24 '13 at 6:57
source share

Another solution is

 class T1 { enum { t = 100 }; public: T1(); }; 

So, t is initialized to 100, and it cannot be changed, and it is closed.

+5
Nov 18 '14 at 10:46
source share

If the element is an array, it will be a little more complicated than normal:

 class C { static const int ARRAY[10]; public: C() {} }; const unsigned int C::ARRAY[10] = {0,1,2,3,4,5,6,7,8,9}; 

or

 int* a = new int[N]; // fill a class C { const std::vector<int> v; public: C():v(a, a+N) {} }; 
+3
01 Oct. '16 at 4:35
source share

Another possible way are namespaces:

 #include <iostream> namespace mySpace { static const int T = 100; } using namespace std; class T1 { public: T1() { cout << "T1 constructor: " << mySpace::T << endl; } }; 

The disadvantage is that other classes can also use constants if they include a header file.

+2
Mar 12 '16 at 18:27
source share

If you do not want the const data member in a static class, you can initialize the const data element using the class constructor. For example:

 class Example{ const int x; public: Example(int n); }; Example::Example(int n):x(n){ } 

If the class has several const data members, you can use the following syntax to initialize the members:

 Example::Example(int n, int z):x(n),someOtherConstVariable(z){} 
+2
Jun 03 '17 at 20:52 on
source share

you can add static to make it possible to initialize this member variable of the class.

 static const int i = 100; 

However, it is not always good practice to use declarations within a class, because all objects created from this class will have the same static variable, which is stored in internal memory outside the memory area of ​​object instances.

0
May 19 '17 at 14:46
source share



All Articles