How do I make an unreferenced object load in C++?

前端 未结 8 1847
一向
一向 2021-01-03 16:36

I have a .cpp file (let\'s call it statinit.cpp) compiled and linked into my executable using gcc. My main() function is

8条回答
  •  清酒与你
    2021-01-03 17:14

    One C++'ish way to do this is with Singletons.

    Essentially, write a function to return a reference to the object. To force it to initialize, make it a static object inside the function.

    Make a class static function that is vaguely like this:

    class MyClass {
       static MyClass& getObject()
       {
            static MyObject obj;
            return obj;
        }
    };
    

提交回复
热议问题