Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to initialize static data members in C++ (98, 11 and 14)

What is the right way to initialize static data members in C++? I'm also interested in how it has changed from C++98, to C++11 to C++14.

Here is an example:

// bufferedOutput.h class BufferedOutput {  // Static member declaration.  static long bytecount; };  // bufferedOutput.cpp long BufferedOutput::bytecount = 50; 

Are there other ways to initialize static data members?

like image 307
bodacydo Avatar asked Oct 30 '14 09:10

bodacydo


People also ask

How do you initialize static data members in C++?

Static Data Member Initialization in C++ We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator, then the variable name. Now we can assign some value.

How do you initialize a static member?

The initializer for a static data member is in the scope of the class declaring the member. A static data member can be of any type except for void or void qualified with const or volatile . You cannot declare a static data member as mutable . You can only have one definition of a static member in a program.

How are static variables initialized in C?

In C, static variables can only be initialized using constant literals. For example, following program fails in compilation. If we change the program to following, then it works without any error.

How are static variables initialized?

Static variables are initialized only once , at the start of the execution. These variables will be initialized first, before the initialization of any instance variables. A single copy to be shared by all instances of the class. A static variable can be accessed directly by the class name and doesn't need any object.


1 Answers

The rules have always been as follows:

  • A const static data member (SDM) of integral or enumeration type can be initialised in class with a constant expression.

  • A constexpr SDM must be initialised in class with a constant expression.

    C++17 no longer requires an initializer when the default constructor initialises every member. Also, constexpr SDMs are implicitly inline variables, which makes their declaration a definition (external definitions are now deprecated).

  • Other kinds of SDMs can have an initializer at their definition (which is in class if that SDM is declared inline).

Nothing has substantially changed between C++03 and C++11+ for code that is valid in both languages.

Note that for SDMs that are not inline, the in-class declaration is not a definition—regardless of whether an initializer is provided—and they must be defined if they are odr-used.

As of C++17, we can make your SDM inline, which makes its in-class declaration a definition:

class BufferedOutput {   static inline long bytecount = 50; }; 
like image 108
Columbo Avatar answered Oct 05 '22 23:10

Columbo