In C++ what would be the best way to have an object that needs to be initalized in main(), but has to be global, so it can be accessed from other functions throughout the program? And make sure the destructor is called to make sure it gets cleaned up properly?
Share
You can also use
std::reference_wrapperif you don’t want to access members viaoperator->.But really, don’t do that. Pass it along if it’s needed, instead of making it global, e.g.