不能被继承的类
时间:2011-04-05 来源:moonApple
题目:用C++设计一个不能被继承的类
思路是将类中的构造函数与析构函数声明为私有的,代码如下:
#include<iostream> using namespace std; class MyFinal{ public: static MyFinal* getInstance(){ cout<<"get the instance"<<endl; return new MyFinal; } static void deleteInstance(MyFinal * myfinal){ cout<<"delete the instance"<<endl; delete myfinal; } private: MyFinal(){} ~MyFinal(){} }; int main(void){ MyFinal *myfinal=MyFinal::getInstance(); MyFinal::deleteInstance(myfinal); return 0; }
相关阅读 更多 +