0

类 BaseA{}; , 类 DerivedB{}; 类派生C{};

DerivedB 可以从 BaseA 派生:想要允许:class DerivedB: public BaseA

但是,如果有人试图做 DerivedC : public BaseA ==> 想要限制这一点。

可能吗?

4

1 回答 1

6

您可以将所有BaseA的构造函数设为私有并添加DerivedB为 a friendof 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
{
};
于 2012-05-25T20:33:47.040 回答