I want my C ++ class to never be created before main () is entered. Is there any way to achieve this?
-
Some explanations:
I am writing an embedded application. My class should be static (located in BSS), but when creating an instance, it requires some resources that are not available before some things were initialized at the beginning of main (). Therefore, I want to make it a singleton Meyers. Ideally, I would like to make some statement that ensures that MyClass :: instance () is never called before main ().
, , - , MyClass::enableConstruction(), . c'tor , , . , , - .
MyClass::enableConstruction()
, . - , , , .
- . , main(). , ? , , .
main()
: CTQ, , , , , , . , , . ::instance(), bool, , , .
::instance()
. , - , , , , ++ ( ). - .
bool, , main()
factory , main()
. , - , .
, , .
, main(), ββ:
bool wasMain (bool inMain = false) { static bool passedMain = false; return passedMain |= inMain; }
wasMain (true), true , false .
: , :
bool wasMain (bool inMain = false) { static bool passedMain = false; if (inMain) passedMain = true; return passedMain; }
, , main. .
MyClass * g_thing = 0; int main() { g_thing = new MyClass(); }
, , . ?
Source: https://habr.com/ru/post/1724755/More articles:StackOverflowError during Building workspace - javaWhy can't my ASP.NET web service start the process, but my .NET console application can? - c #determine step, yaw, throw from front to top - openglload a new page using ajax - ajaxUsing a queue as a data source - c #Can someone explain the difference between XMLRPC, SOAP, and also C # web service? - pythonHow to transfer data between BLL and UI in a three-layer (single-level) application? - user-interfaceValidating a class using DataAnnotations - c #collision name in C ++ - c ++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1724760/pass-by-value-or-reference-to-a-c-constructor-that-needs-to-store-a-copy&usg=ALkJrhiq5ly1Bo5eyN9WxdI371-V4sNztAAll Articles