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
Kolokwium_2011_z7
Graph Theory 2
map::swap()_30-Seconds-of-C++
simple use of templete
basic TREAP
Policy based smart pointer
C++ Solar eclipse program(Shoushi integrated) 1644 - 1785 from Ideone( Date: August 14, 2014 )
TraiectorieIdeala
HashMap
C++ Solar eclipse program(Shoushi integrated) 1568 - 1644 also check Lunar eclipse for Xing Yunlu