BaseA {}类; ,类DerivedB {};类DerivedC {};
DerivedB可以从BaseA派生:允许:类DerivedB:公共BaseA
但是,如果有人尝试执行DerivedC:public BaseA ==>希望对此进行限制。
可能吗?
最佳答案
您可以将所有BaseA
的构造函数设为私有,并将DerivedB
添加为friend
的BaseA
。
class BaseA
{
friend class DerivedB;
private:
BaseA();
};
class DerivedB : BaseA
{
};
class DerivedC : BaseA //error - I get it when I try to create an object in MSVS
//but in principle it works
{
};
关于c++ - 限制继承,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10760997/