Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static constructors in C++? I need to initialize private static objects

People also ask

Can we have private static constructor in C?

A class can have only one static constructor. Private Constructor - This is the constructor whose access modifier is private. private constructor is used to prevent a class to be instantiated.

How do I initialize private static?

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. The following code will illustrate the of static member initializing technique.

How do you initialize a static object?

As static variables are initialized only once and are shared by all objects of a class, the static variables are never initialized by a constructor. Instead, the static variable should be explicitly initialized outside the class only once using the scope resolution operator (::).

Can a static variable be initialized in a constructor C#?

You can define a static field using the static keyword. If you declare a static variable in a class, if you haven't initialized it, just like with instance variables compiler initializes these with default values in the default constructor. Yes, you can also initialize these values using the constructor.


To get the equivalent of a static constructor, you need to write a separate ordinary class to hold the static data and then make a static instance of that ordinary class.

class StaticStuff
{
     std::vector<char> letters_;

public:
     StaticStuff()
     {
         for (char c = 'a'; c <= 'z'; c++)
             letters_.push_back(c);
     }

     // provide some way to get at letters_
};

class Elsewhere
{
    static StaticStuff staticStuff; // constructor runs once, single instance

};

Well you can have

class MyClass
{
    public:
        static vector<char> a;

        static class _init
        {
          public:
            _init() { for(char i='a'; i<='z'; i++) a.push_back(i); }
        } _initializer;
};

Don't forget (in the .cpp) this:

vector<char> MyClass::a;
MyClass::_init MyClass::_initializer;

The program will still link without the second line, but the initializer will not be executed.


C++11 solution

Since C++11, you can simply use lambda expressions to initialize static class members. You can impose an order of construction between the various static members, and even declare your static members as const.

Header file:

class MyClass {
    static const vector<char> letters;
    static const size_t letterCount;
};

Source file:

// Initialize MyClass::letters with all letters from 'a' to 'z'.
const vector<char> MyClass::letters = [] {
    vector<char> letters;
    for (char c = 'a'; c <= 'z'; c++)
        letters.push_back(c);
    return letters;
}();

// Initialize MyClass::letterCount based on MyClass::letters. Static members are
// always initialized in the exact same order as defined within the source file.
const size_t MyClass::letterCount = letters.size();

In the .h file:

class MyClass {
private:
    static int myValue;
};

In the .cpp file:

#include "myclass.h"

int MyClass::myValue = 0;