Click here to Skip to main content
13,351,985 members (88,259 online)
Click here to Skip to main content
Add your own
alternative version


28 bookmarked
Posted 27 Nov 2011

Static Constructor in C++

, 27 Nov 2011
Rate this:
Please Sign up or sign in to vote.
An easy way to implement static constructors and static destructors in standard C++.


Once you start to optimize your code, read about patterns, and so on, you realize it is an interesting idea using static data members to keep a single copy of something you may use in all the instances of your class (instead of having a copy in each object). Sometimes those static data members are very simple, like constants or others, but when that information gets more and more complex, then you realize on the limitations of the C++ language:

  • Static data members must be initialized outside the class body, generally in *.cpp files, except const integral-type static data members. It is a great inconvenience for inline classes and templates defined in one *.h file.
  • C++ doesn't have static constructors, as Java or C# does, so you usually have to initialize the static data members one by one (independently). This is a limitation because you may want to initialize several static data members in the same loop or algorithm, for example.

I decided to workaround both limitations, and here is the result.


I previously discoursed the first limitation in this topic at Stack Overflow: static constructors in C++? need to initialize private static objects.

About the second limitation, it is interesting to read this other topic: What is the rationale for not having static constructors in C++?. Here is my point of view on this:

  • Although C++ was not intended to have static constructors in the beginning, it is still interesting to have the possibility to extend the language to work in the same way as Java and C#, as it is the free will of the programmer to use that or not.
  • C++ is an older language than Java and C#, static constructors were invented later, but there is no technical reason why they cannot be implemented now.
  • There are ways to call functions to initialize static data members one by one, but it is not the same as initializing several static data members at the same time in the same algorithm (in a static constructor for example). That can also be done in a static Init() function member... just name it StaticConstructor() and create a mechanism to call it automatically at startup and you got it!
  • Java and C# are managed languages, so they do not need static destructors. But native C++ is different. As a non-managed language, it should have a destructor for each constructor, as there is no garbage collector in native C++. If the programmer calls new at the constructor, we should be able to call delete at the destructor. That is the reason I also implemented the static destructor.

Using the Code

The way of using the code is very simple:

  • Include the provided StaticConstructor.h.
  • Declare StaticConstructor and StaticDestructor as static function members of your class.
  • Invoke them with the macro outside of the class body.
#include <StaticConstructor.h>

class MyClass
    // Declare here static and non-static data members...
    // Default Constructor:
        // Do some stuff here with non-static data members...

    // Destructor:
    virtual ~MyClass()
        // Do some stuff here with non-static data members...

    // Static Constructor:
    // (Should be called by INVOKE_STATIC_CONSTRUCTOR macro)
    static void StaticConstructor()
        // Do some stuff here with static data members...

    // Static Destructor:
    // (Should be called by INVOKE_STATIC_CONSTRUCTOR macro)
    static void StaticDestructor()
        // Do some stuff here with static data members...

// Invoke the StaticConstructor & StaticDestructor of the class:
// Make sure you put this AFTER the initialization of the static data members!

You may also use the macros STATIC_CONSTRUCTOR() and STATIC_DESTRUCTOR() as an alias for static void StaticConstructor() and static void StaticDestructor() respectively, but this only works for inline classes (usually declared only in .h files) or the header declaration.

To declare the implementation in a .cpp file, you should use the full expressions static void MyClass::StaticConstructor() and static void MyClass::StaticDestructor(). The idea is also to invoke the static constructor in only one .cpp file (whenever possible) to avoid several invokes to it. You may download the source and examples of all these.

Apart from that, I also implemented macros for a fast static start-up code (without the need to declare a static constructor in a dummy class):

#include <StaticConstructor.h>

// Sample code for StartUp
    std::cout << "Starting up..." << std::endl;

// Sample code for FinishUp
    std::cout << "Finishing up..." << std::endl;

Using Templates

Using this kind of static constructor in templates is possible, but it is important to invoke separately the static constructor of each template instance in this way:

// Declaration of alias (class names) for the template instaces:
typedef MyTemplate<int>    MyTemplateInt;
typedef MyTemplate<double> MyTemplateDouble;

// Invoke the StaticConstructor & StaticDestructor for each one of the template instances:
// Should be called with alias (class names) for the template instaces.

This is usually done in a different .cpp file from the template definition, which is usually in a .h file. If you also need to initialize some data members for the template, you may do it in the same .cpp file, also for each template instance... but this could be a great inconvenience. You may prefer to encapsulate the initialization inside the template code, because:

  • You keep together the template code and its initialization.
  • You do not need to repeat the initialization code for each instance of the template.
  • You may not want to put that code in a .cpp file.

But this cannot be done in C++, so I tried a workaround. Instead of using a data member, I used a function member to store data. I called this: "data function member" (DF member), whose implementation is:

static TypeName& DFMemberName()
    static TypeName DFMemberName(InitValue);
    return DFMemberName;

In the examples, I use the macro STATIC_DF_MEMBER(TypeName, DFMemberName, InitValue) to declare them in an easier way. You can declare the DF members in the class or template header declaration, and access them in the static constructor and the static destructor. You may use them as a reference to your data members (in fact, they are function members that return references to your data), so this kind of code is valid: DFMemberName() = Value2;.

The DF members are initialized the first time you access them, not when you declare them (they are not normal data members), so you may want to access them at the static constructor to ensure they are initialized at the beginning of the execution.

Points of Interest

Implementation: I used a trick in the macro that invokes the static constructor - I declare a global variable (that would be constructed at startup) to put some code in its default constructor (a call to the static constructor of your class). The destructor of that class would call the static destructor of your class.

Order problem: The compiler will process the calls to the data member initialization and the static constructors in a particular order that could be compiler-dependant. Be very careful with that, otherwise the data members may not be initialized the way you want. In Microsoft Visual Studio, the order seems to be the order of the lines of code in the file, so make sure you put the call to INVOKE_STATIC_CONSTRUCTOR after the data member initialization.


  • v1.2 - 2011/11/27: First release with some improvements and a full example.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Hugo González Castro
Software Developer (Senior)
Spain Spain
B.Sc. Mathematics and Computer Science.
Programming in C++ since 2003.

You may also be interested in...

Comments and Discussions

Questionvirtual constructor C++ Pin
panqnik12-Mar-14 13:03
memberpanqnik12-Mar-14 13:03 
AnswerRe: virtual constructor C++ Pin
Hugo González Castro12-Mar-14 15:26
memberHugo González Castro12-Mar-14 15:26 
QuestionStatic constructors Pin
Sergey Chepurin5-Dec-11 23:15
memberSergey Chepurin5-Dec-11 23:15 
General[My vote of 2] Why not a class to implement static constructor ? My vote is 2 Pin
Wonixen30-Nov-11 17:13
memberWonixen30-Nov-11 17:13 
GeneralRe: [My vote of 2] Why not a class to implement static constructor ? My vote is 2 Pin
Hugo González Castro1-Dec-11 3:31
memberHugo González Castro1-Dec-11 3:31 
GeneralRe: [My vote of 2] Why not a class to implement static constructor ? My vote is 2 Pin
Wonixen1-Dec-11 3:46
memberWonixen1-Dec-11 3:46 
GeneralRe: [My vote of 2] Why not a class to implement static constructor ? My vote is 2 Pin
Hugo González Castro2-Dec-11 16:51
memberHugo González Castro2-Dec-11 16:51 
GeneralRe: [My vote of 2] Why not a class to implement static constructor ? My vote is 2 Pin
Wonixen3-Dec-11 6:18
memberWonixen3-Dec-11 6:18 
GeneralRe: [My vote of 2] Why not a class to implement static constructor ? My vote is 2 Pin
Hugo González Castro6-Dec-11 2:37
memberHugo González Castro6-Dec-11 2:37 
GeneralMy vote of 1 Pin
bosedk30-Nov-11 1:51
memberbosedk30-Nov-11 1:51 
GeneralRe: My vote of 1 Pin
Hugo González Castro30-Nov-11 6:51
memberHugo González Castro30-Nov-11 6:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 27 Nov 2011
Article Copyright 2011 by Hugo González Castro
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid