Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
constructing object on first use as return value of (pointer to) object-returning function
//see also: https://rextester.com/CTC97213 //constructing object on first use as return value of (pointer to) object-returning function // no need for the "application()->close()" anymore... //g++ 5.4.0 //Coded by Twareintor (2020) #include <iostream> #include <memory> class CxxApplication { friend CxxApplication* application(); static std::unique_ptr<CxxApplication> inst; public: CxxApplication(){std::cout<<"app opened\n";} ~CxxApplication(){std::cout<<"app closed\n";} void perform(){std::cout<<"app performing ... done\n";} void perform(int i){std::cout<<"app performing ... "<< i <<" times\n";} // void close(){if(inst) delete inst;} // no more needed }; std::unique_ptr<CxxApplication> CxxApplication::inst(nullptr); CxxApplication* application() { if(!CxxApplication::inst.get()) CxxApplication::inst = std::unique_ptr<CxxApplication>(new CxxApplication()); return CxxApplication::inst.get(); } int main() { application()->perform(); application()->perform(30); // application()->close(); // no more needed return 0; }
run
|
edit
|
history
|
help
0
hilbert
a
stack and queue
variadic pointer to function template
Tubee c++
UtilityPair
Find the Duplicate Number in array of n+1 integers having elements from 1 to n
Parenthesis checker
MY NEW
Vector+-OpLoad