template< typename TDerive, typename TProvider >
class CFobidDeriveProviderBase
{
friend TDerive;
friend TProvider;
private:
CFobidDeriveProviderBase(){}
~CFobidDeriveProviderBase(){}
};
/*
* 提供禁止派生的功能,需要此功能的类可以从CFobidDeriveProvider派生,并将类名作为模板参数传递
*/
template< typename TDerive >
class CFobidDeriveProvider : virtual public CFobidDeriveProviderBase< TDerive, CFobidDeriveProvider<TDerive>>
{
public:
CFobidDeriveProvider(){}
~CFobidDeriveProvider(){}
};
/*
* 测试类,该类不可被继承
*/
class CNoDerive : public CFobidDeriveProvider< CNoDerive >
{
public:
CNoDerive(){}
~CNoDerive(){}
void Alert()
{
AtlMessageBox( NULL, _T("Alert") );
}
};
之所以将继承的结构分为2层:CFobidDeriveProvider和CFobidDeriveProviderBase,主要是方便使用,用户只需直接从CFobidDeriveProvider派生就可实现一个不可被继承的类,而不需要虚拟继承。
若有类从CNoDerive派生:
class CSomeDerive : public CNoDerive
{
public:
CSomeDerive(){}
~CSomeDerive(){}
};