Click here to Skip to main content
Licence CPOL
First Posted 7 Apr 2007
Views 30,560
Downloads 89
Bookmarked 13 times

Static Initialization Function in Classes

By | 1 Oct 2008 | Article
Automaticlly invoke static initialization functions of classes.

Content

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 {
public:
    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 {
public:
    static vector<string> stringList;
private:
    static bool __init;
    static bool init() {
        stringList.push_back("string1");
        stringList.push_back("string2");
        stringList.push_back("string3");

        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.

License

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

About the Author

jamesfancy



China China

Member

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:
http://hi.baidu.com/jamesfancy

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
BugIt doesn't work with optimization PinmemberMember 869768721:46 10 Apr '12  
GeneralThanks. Very useful! PinmemberAbdusalam Abdurahman18:32 19 Jan '10  
GeneralRe: Thanks. Very useful! Pinmemberjamesfancy23:12 21 Jan '10  
GeneralStatic Initialization Function in Classes Pinmemberwtu_ben15:13 5 Oct '08  
GeneralRe: Static Initialization Function in Classes Pinmemberjamesfancy20:26 5 Oct '08  
Generalyour English is just so-so PinmemberMember 447877821:51 2 Oct '08  
GeneralRe: your English is just so-so Pinmemberjamesfancy17:04 3 Oct '08  
QuestionHow I usually do it [modified] PinmemberSnakefoot1:10 2 Oct '08  
AnswerRe: How I usually do it Pinmemberjamesfancy16:57 2 Oct '08  
AnswerRe: How I usually do it PinmemberFarid Z6:54 7 Dec '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 2 Oct 2008
Article Copyright 2007 by jamesfancy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid