我想生成一个全局的A类的对象,让它能在不同类,如B,C,D的方法中使用,等用完再销毁,如何实现呢?谢谢
解决方案
一般使用静态的单例类来实现
class A{ private: A() { //do whatever } public: static A getInstance() { return instance; } void print(){cout << "hello";} ~A(){} private: static A instance;};int main(){ A a = A::getInstance(); a.print(); }