如何实现不可继承的类
.NET的类可以用 sealed 关键字来说明一个类是最终类,也就是说不能作为基类,不能被继承。Java也有个相同的关键字final完成同样的功能。这个功能有时候很有用。但是C++没有这个关键字。那如何在C++中实现类似的功能呢?
如何实现不可继承的类 沐枫网志
曾经在CUJ上看到一篇文章介绍过一个方法,但具体网址已经忘记了,Sorry。但是方法却记下来了。它用一个私有的虚基类来实现。
我在这种方法的基础上,作了一个通用的工具类:Sealed,只要以此类为基类,那么该类就不可被继承。为了进行性能优化,在Release版本中,还将Sealed作成一个空类。
Sealed类的实现如下:
#ifdef _DEBUG
namespace internalSealed
{
class Class_Is_Sealed
{
protected:
Class_Is_Sealed(){};
};
template <class T>
class typewrapper
{
public:
typedef T type;
};
};
template <typename T>
class Sealed: private virtual internalSealed::Class_Is_Sealed
{
#ifdef _MSC_VER
friend T;
#else
friend class internalSealed::typewrapper<T>::type;
#endif
};
#else
template <typename T>
class Sealed
{
};
#endif
namespace internalSealed
{
class Class_Is_Sealed
{
protected:
Class_Is_Sealed(){};
};
template <class T>
class typewrapper
{
public:
typedef T type;
};
};
template <typename T>
class Sealed: private virtual internalSealed::Class_Is_Sealed
{
#ifdef _MSC_VER
friend T;
#else
friend class internalSealed::typewrapper<T>::type;
#endif
};
#else
template <typename T>
class Sealed
{
};
#endif
这样子,在Debug方式下,只要一个类从Sealed
同时,在Release方式下,因为不再检查是否可以被继承,因而不产生开销(空类会被编译器优化掉)。
使用例子:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)