Click here to Skip to main content
13,865,833 members
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 7 Apr 2007
Licenced CPOL

Static Initialization Function in Classes

, 1 Oct 2008
Rate this:
Please Sign up or sign in to vote.
Automaticlly invoke static initialization functions of classes.


Sometimes when we write a C++ class, we want a static function to be used to initialize the class. For example, a static function is used to initialize a static vector or map. We can do the same thing easily in Java, by Java's static block. How we can do with C++?

At first, let's see how to initialize a normal static member in C++.

class Test1 {
    static string emptyString;

string Test1::emptyString = "";
// also can be
// string Test1::emptyString;
// string Test1::emptyString("");

As you saw, we declare a static member variable in the class definition, then define and initialize it in the implementation file (.cpp).

Now we meet the first problem, how to initialize a static collection member? The answer is we must write a static function to do that.

And now we meet the second problem, how to invoke the initialization function? Normally, we will invoke the initialization function in the main function. But I think it's not a good solution because users may forget to invoke the initialization function.

Here is my solution:

class Test2 {
    static vector<string> stringList;
    static bool __init;
    static bool init() {

        return true;

// Implement
vector<string> Test2::stringList;
bool Test2::__init = Test2::init();

An additional static member is used in my solution. When the additional static member is initialized, the initialization function is invoked automatically.


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


About the Author

China China
James Fancy, is a software engineer from China. He enjoys and familiar with Java, C++ and some script languages.

If you can read Chinese word, here is one of James Fancy's BLOG:

You may also be interested in...

Comments and Discussions

BugIt doesn't work with optimization Pin
Member 869768710-Apr-12 22:46
memberMember 869768710-Apr-12 22:46 
GeneralThanks. Very useful! Pin
Abdusalam Abdurahman19-Jan-10 19:32
memberAbdusalam Abdurahman19-Jan-10 19:32 
GeneralRe: Thanks. Very useful! Pin
jamesfancy22-Jan-10 0:12
memberjamesfancy22-Jan-10 0:12 
GeneralStatic Initialization Function in Classes Pin
wtu_ben5-Oct-08 16:13
memberwtu_ben5-Oct-08 16:13 
GeneralRe: Static Initialization Function in Classes Pin
jamesfancy5-Oct-08 21:26
memberjamesfancy5-Oct-08 21:26 
Generalyour English is just so-so Pin
Member 44787782-Oct-08 22:51
memberMember 44787782-Oct-08 22:51 
GeneralRe: your English is just so-so Pin
jamesfancy3-Oct-08 18:04
memberjamesfancy3-Oct-08 18:04 
QuestionHow I usually do it [modified] Pin
Rolf Kristensen2-Oct-08 2:10
memberRolf Kristensen2-Oct-08 2:10 
AnswerRe: How I usually do it Pin
jamesfancy2-Oct-08 17:57
memberjamesfancy2-Oct-08 17:57 
AnswerRe: How I usually do it Pin
Farid Z7-Dec-09 7:54
memberFarid Z7-Dec-09 7:54 

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 | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 2 Oct 2008
Article Copyright 2007 by jamesfancy
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid