不能被继承的类

题目:用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;
}

posted @ 2011-04-05 22:34  akawhy  阅读(466)  评论(0编辑  收藏  举报