Global variables and scope - C++

前端 未结 4 540
北荒
北荒 2021-01-01 02:57

I am having small problem in making a global variable works. I am using Visual Studio 2008 and standard C++.

I have two projects, one is a static library and second

4条回答
  •  青春惊慌失措
    2021-01-01 03:41

    Don't use global variables. Just don't. Much better, if you HAVE to have globally accessible data, is to use a global function which will return globalWord, like this:

    std::string globalWord()
    {
        static std::string word("Hi Mom");
        return word;
    }
    

    This saves you from initialization order issues (read Effective C++ item #4).

提交回复
热议问题