77

This question was asked to me in an interview:

What is a static constructor?

Does it exist in C++? If yes, please explain it with an example.

Rakete1111
  • 47,013
  • 16
  • 123
  • 162
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 19
    Looks like you missed the C++ interview and attended the java/C# interview ;) – Alok Save Apr 27 '11 at 12:23
  • possible duplicate of [What is the rationale for not having static constructor in C++?](http://stackoverflow.com/questions/5301666/what-is-the-rationale-for-not-having-static-constructor-in-c) – sbi Apr 27 '11 at 13:01
  • 1
    If it's addressed to a C++ programmer, it should *explain* not ask what's meant by static constructor as it's not C++ terminology. For example, it might be asking if the constructor could have static linkage, or could be prefixed with the static keyword to some achieve unspecified behaviour.... – Tony Delroy Apr 28 '11 at 07:05
  • 1
    possible duplicate of [static constructors in C++? need to initialize private static objects](http://stackoverflow.com/questions/1197106/static-constructors-in-c-need-to-initialize-private-static-objects) – Mark Hurd Dec 17 '12 at 15:19
  • 1
    possible duplicate of [Why can't constructor be declared as static in c++?](http://stackoverflow.com/questions/17500821/why-cant-constructor-be-declared-as-static-in-c) – D Mehta Oct 11 '14 at 04:37

13 Answers13

96

C++ doesn’t have static constructors but you can emulate them using a static instance of a nested class.

class has_static_constructor {
    friend class constructor;

    struct constructor {
        constructor() { /* do some constructing here … */ }
    };

    static constructor cons;
};

// C++ needs to define static members externally.
has_static_constructor::constructor has_static_constructor::cons;
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 1
    I guess that `class constructor` should be a friend of the `class has_static_constructor` to do anything useful? Otherwise it's just yet another static member. – davka Apr 27 '11 at 13:33
  • @davka good remark. In fact, nesting isn’t particularly useful (other than constraining the scope, always a good thing). – Konrad Rudolph Apr 27 '11 at 13:52
  • @Konrad: Is it okay to use `class` in friend declaration, and `struct` in the definition? – Nawaz May 16 '11 at 14:31
  • @Nawaz The compiler might warn (depending on the settings) but this is absolutely OK. I only used it because `friend struct` unfortunately doesn’t compile and I was too lazy to put `public:` into the inner class. However, In real code I would definitely strive for uniformity. – Konrad Rudolph May 16 '11 at 14:41
  • @Konrad: Why doesn't `friend struct` compile? Is it not allowed? :-/ – Nawaz May 16 '11 at 14:45
  • 1
    @Nawaz According to §7.1.5.3, it is. But GCC threw it back at me. – Konrad Rudolph May 16 '11 at 15:07
  • 13
    You do have to be *VERY CAREFUL* about what you put in such a non-local static object constructor. When this constructor will run is non-deterministic and it will be very early in the boot process. A thorough explanation can be found in Scott Meyer's Effective C++ (Item 47 in the 2nd edition) on NLSO initialization. This can really bite you in the embedded world where the RTOS won't be available when the constructor runs. – Tod Apr 25 '12 at 21:14
  • 2
    @Tod Very valid comment. Unfortunately, having lazy loading is quite a bit more complicated. Essentially I would solve it with a static `unique_ptr` to a nested class which holds all the “static” members as, in fact, non-static members, and which is initialised to 0 and `reset` to a valid pointer once it’s first accessed. – Konrad Rudolph Apr 25 '12 at 22:06
  • If `has_static_constructor` is a template the static constructor is not called. This can be fixed by adding `public: has_static_constructor() { (void)cons; }`. In that case a static constructor will be called for every instance of this template. – Scindix May 29 '18 at 11:31
20

In C++, there is no static constructor. In C# (and probably in Java too), you can define static constructor which is called automatically by the runtime so as to initialize static members.

For further question and interest you can read this topic:

What is the rationale for not having static constructor in C++?

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
10

Since we do not technically have static constructors in C++, you have to decide whether it is worth it to do something tricky to force the issue (e.g. using a static instance of a nested class), or to just slightly restructure your code to call a static initializer early in your program's life.

#include <iostream>           // cout, endl

class Foo {
   public:
      static int s_count;

      // Constructor definition
      Foo (int l, int w, int h)
      {
         cout <<"Foo ctor called." << endl;
         length = l;
         width  = w;
         height = h;

         // Increase every time object is created
         s_count++;
      }

      int vol ()
      {
         return length * width * height;
      }

      static void initCount()
      {
         s_count = 0;
      }

      static int getCount()
      {
         return s_count;
      }

   private:
      double length;     // Length of a box
      double width;      // Width  of a box
      double height;     // Height of a box
};

// Initialize static member of class Foo
int Foo::s_count;  // Initializing here is non-deterministic

int main(void) {

   Foo::initCount();  // Initializing here is deterministic

   // Print total number of objects before creating object.
   cout << "Inital Count: " << Foo::getCount() << endl;

   Foo Foo1(3, 1, 1);    // Declare box1
   Foo Foo2(8, 6, 2);    // Declare box2

   // Print total number of objects after creating object.
   cout << "Final Count: " << Foo::getCount() << endl;

   return 0;
}

Output:

$ static_init_test
Inital Count: 0
Foo ctor called.
Foo ctor called.
Final Count: 2

I like this approach better; as a silver lining, it takes the non- out of non-deterministic initialization.

There is one gotcha though -- this technique is insufficient if you are trying to initialize static const variables. For static const variables, you will have to make them private to the class and provide getters for outsiders to read them.

Note: I updated this code -- it compiles and runs successfully with no warnings via:

g++ static_init_test.cpp -std=c++11 -o static_init_test
bearvarine
  • 661
  • 6
  • 10
  • 1
    This is by far the BEST answer to the eternal questions about how/why/if/when to use some tricks to emulate an static constructor in C++. – jose.angel.jimenez Sep 03 '14 at 07:29
  • @bearvarine As for your recent question on Meta-SO. Yes, this is one of the questions, that won't survive at SO nowadays. I'm close voting many questions of this form everyday Because I think their value for future researchers is minor (or there is a duplicate already), and the OP has been missing to research the site before asking their own question. The moderation tone has changed, especially for questions kind of this caliber, since the answers are already ubiquitously available meanwhile. – πάντα ῥεῖ Mar 31 '15 at 18:43
  • This leads to the following Error in VS2017: Severity Code Description Project File Line Suppression State LNK2001 unresolved external symbol "public: static class std::basic_string,class std::allocator > Foo::my_text_str" (?my_text_str@Foo@@2V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A) – Harsh Kumar Narula Jul 29 '17 at 12:26
  • I have just hit this problem. And your solution is not always possible in the real world projects where you did not write the code from scratch and all by yourself. – Alex D Sep 28 '18 at 22:32
8

Static constructors exist in C# and Java.
They are used to initialize static members of a class.
The runtime executes them before the class is first used.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
4

There is no such thing in C++. Constructors and destrcutors typically used to create or destruct instance of object. It's meaningless to call them without corresponding object instance. You can emulate them using a singleton pattern.

beduin
  • 7,943
  • 3
  • 27
  • 24
3

A static constructor is used to initialize static data of a class. C++ doesn't have static constructor. But a static constructor can be emulated by using a friend class or nested class as below.

class ClassStatic{
private:
    static char *str;
public:
    char* get_str() { return str; }
    void set_str(char *s) { str = s; }
    // A nested class, which used as static constructor
    static class ClassInit{
    public:
        ClassInit(int size){ 
            // Static constructor definition
            str = new char[size];
            str = "How are you?";
        }
    } initializer;
};

// Static variable creation
char* ClassStatic::str; 
// Static constructor call
ClassStatic::ClassInit ClassStatic::initializer(20);

int main() {
    ClassStatic a;
    ClassStatic b;
    std::cout << "String in a: " << a.get_str() << std::endl;
    std::cout << "String in b: " << b.get_str() << std::endl;
    a.set_str("I am fine");
    std::cout << "String in a: " << a.get_str() << std::endl;
    std::cout << "String in b: " << b.get_str() << std::endl;
    std::cin.ignore();
}

Output:

String in a: How are you?
String in b: How are you?
String in a: I am fine
String in b: I am fine
Jobin
  • 6,506
  • 5
  • 24
  • 26
2

May be they mean this:

class Cat
{
private:
Cat();
public:
static Cat getCat() {return Cat(); }
}
1

I think static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced. In++, we dont have anything called static constructor but you can mimic the functionality of the static constructor. Take a look at this C# static constructor:

public class Bus  {
     // Static variable used by all Bus instances.
     // Represents the time the first bus of the day starts its route.
     protected static readonly DateTime globalStartTime;

     // Property for the number of each bus.
     protected int RouteNumber { get; set; }

     // Static constructor to initialize the static variable.
     // It is invoked before the first instance constructor is run.
     static Bus()
     {
         globalStartTime = DateTime.Now;

         // The following statement produces the first line of output, 
         // and the line occurs only once.
         Console.WriteLine("Static constructor sets global start time to {0}",
             globalStartTime.ToLongTimeString());
     }

     // Instance constructor.
     public Bus(int routeNum)
     {
         RouteNumber = routeNum;
         Console.WriteLine("Bus #{0} is created.", RouteNumber);
     }

     // Instance method.
     public void Drive()
     {
         TimeSpan elapsedTime = DateTime.Now - globalStartTime;

         // For demonstration purposes we treat milliseconds as minutes to simulate
         // actual bus times. Do not do this in your actual bus schedule program!
         Console.WriteLine("{0} is starting its route {1:N2} minutes after global start time {2}.",
                                 this.RouteNumber,
                                 elapsedTime.Milliseconds,
                                 globalStartTime.ToShortTimeString());
     }  }

 class TestBus  {
     static void Main()
     {
         // The creation of this instance activates the static constructor.
         Bus bus1 = new Bus(71);

         // Create a second bus.
         Bus bus2 = new Bus(72);

         // Send bus1 on its way.
         bus1.Drive();

         // Wait for bus2 to warm up.
         System.Threading.Thread.Sleep(25);

         // Send bus2 on its way.
         bus2.Drive();

         // Keep the console window open in debug mode.
         System.Console.WriteLine("Press any key to exit.");
         System.Console.ReadKey();
     }  }  /* Sample output:
     Static constructor sets global start time to 3:57:08 PM.
     Bus #71 is created.
     Bus #72 is created.
     71 is starting its route 6.00 minutes after global start time 3:57 PM.
     72 is starting its route 31.00 minutes after global start time 3:57 PM.      
*/
1

In C++, there is no such thing as a static constructor.

Gilad Naor
  • 20,752
  • 14
  • 46
  • 53
1

Here is the simplest static constructor in c++17:

#include <iostream>
class h {
    public:
    static inline int i;
    private:
    struct constructor {
        constructor() {
         i=3;
        }
    };
    static inline constructor cons;
};

int main() {
  std::cout << h::i;
}
Lewis Kelsey
  • 4,129
  • 1
  • 32
  • 42
0

See my answer to a similar question. C#'s static-constructor metaphor can be done in C++.

ulatekh
  • 1,311
  • 1
  • 14
  • 19
0

In C++, if someone says "static constructor", they are generally referring to "static initialization" (and destruction). It's not uncommon to use this terminology.

Aaron Graham
  • 231
  • 1
  • 6
-1

Other way to emulate static constructor behaviour is to use instance variable with private constructor and static factory method.

Cat* Cat::give_birth() {
  static Cat *myone = NULL;
  if (myone == NULL) {
    myone = new Cat();
  }
  return myone;
}
dsamersoff
  • 49
  • 5