首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

交流一下怎么实现sealed class

2012-03-23 
大虾交流一下如何实现sealed class?如题![解决办法]可以把构造函数都私有化。但代价是需要提供静态函数来创

大虾交流一下如何实现sealed class?
如题!

[解决办法]
可以把构造函数都私有化。但代价是需要提供静态函数来创建对象。
[解决办法]
c++ 可以这样模拟 密封类


template <typename T>
class Class_Is_Sealed
{
protected:
Class_Is_Sealed(){};
};


template <typename T>
class Sealed: private virtual Class_Is_Sealed <T>
{
friend typename T;
};

class test: Sealed <test>
{
public:
int print()
{
return 1;
}
};

class ttt: public test //, Sealed <test> , Sealed <ttt>
{
public:
int print()
{
return 2;
}
};


int main()
{

test t;
printf( '%d\n ', t.print());

ttt t1;
printf( '%d\n ', t1.print());


system( "pause ");

}


只要一个类从Sealed <T> 继承,就不可再被继承了。


[解决办法]
只不过是C++缺少了final关键字而已。
这个,项目实际控制一下就可以了,没必要搞出太复杂的机制来防止。
几乎不可能完成的任务。

热点排行