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

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.

  

posted on 2013-01-22 16:18  androidme  阅读(183)  评论(0编辑  收藏  举报

导航