c++中公共继承、私有继承和受保护继承之间的区别是什么?
我在SO上找到的所有问题都是针对具体案例的。
c++中公共继承、私有继承和受保护继承之间的区别是什么?
我在SO上找到的所有问题都是针对具体案例的。
当前回答
限制继承的可见性将使代码无法看到某些类继承了另一个类:从派生类到基类的隐式转换将不起作用,从基类到派生类的static_cast也将不起作用。
只有类的成员/友元才能看到私有继承,只有类的成员/友元和派生类才能看到受保护继承。
公共继承
是一种继承。一个按钮就是一个窗口,只要需要一个窗口,就可以传递一个按钮。 类按钮:公共窗口{};
保护继承
implemented-in-terms-of保护。很少有用。在boost::compressed_pair中使用从空类派生并使用空基类优化来节省内存(下面的例子没有使用模板来保持在点上): 结构empty_pair_impl: protected empty_class_1 {non_empty_class_2秒;}; 结构对:private empty_pair_impl { Non_empty_class_2 &second() { 返回这个- >第二; } Empty_class_1 &first() { 返回*;//注意我们返回*this! } };
私有继承
Implemented-in-terms-of. The usage of the base class is only for implementing the derived class. Useful with traits and if size matters (empty traits that only contain functions will make use of the empty base class optimization). Often containment is the better solution, though. The size for strings is critical, so it's an often seen usage here template<typename StorageModel> struct string : private StorageModel { public: void realloc() { // uses inherited function StorageModel::realloc(); } };
公共成员
总 类对{ 公众: 首先第一; 第二第二; }; 访问器 类窗口{ 公众: int getWidth(); };
保护成员
提供对派生类的增强访问 类堆栈{ 保护: 矢量c < >元素; }; 类窗口{ 保护: void registerClass(window_descriptor w); };
私有成员
保持实现细节 类窗口{ 私人: int宽度; };
请注意,c风格强制转换有意允许以已定义和安全的方式将派生类强制转换为受保护或私有基类,也可以强制转换为其他方向。无论如何都应该避免这种情况,因为它会使代码依赖于实现细节——但如果有必要,您可以使用这种技术。
其他回答
公共继承对IS-A关系建模。与
class B {};
class D : public B {};
每个D都是B。
私有继承为IS-IMPLEMENTED-USING关系建模(或者其他什么关系)。与
class B {};
class D : private B {};
D不是B,但每个D都在实现过程中使用了B。私有继承总是可以通过使用包含来消除:
class B {};
class D {
private:
B b_;
};
这个D也可以用B实现,在这种情况下使用它的b_。与继承相比,包含在类型之间的耦合不那么紧密,因此通常应该首选它。有时使用容器代替私有继承不像私有继承那样方便。这通常是懒惰的蹩脚借口。
我想没有人知道什么是受保护的继承模型。至少我还没看到任何令人信服的解释。
私人:
基类的私有成员只能被该基类的成员访问。
公众:
基类的公共成员可以被该基类的成员、其派生类的成员以及基类和派生类之外的成员访问。
保护:
基类的成员可以访问基类的受保护成员,也可以访问其派生类的成员。
简而言之:
私人:基地
Protected:基础+派生
Public: base + derived +任何其他成员
1)公共继承:
a.基类的私有成员在派生类中不可访问。
b.基类的受保护成员在派生类中仍然受保护。
c.基类的公共成员在派生类中保持为公共。
因此,其他类可以通过派生类对象使用基类的公共成员。
2)受保护遗产:
a.基类的私有成员在派生类中不可访问。
b.基类的受保护成员在派生类中仍然受保护。
c.基类的公共成员也成为派生类的受保护成员。
因此,其他类不能通过派生类对象使用基类的公共成员;但它们可用于派生的子类。
3)私人继承:
a.基类的私有成员在派生类中不可访问。
b.基类的Protected & public成员变成派生类的private成员。
因此,基类的成员不能被其他类通过派生类对象访问,因为它们在派生类中是私有的。甚至是Derived的子类 类不能访问它们。
我发现了一个简单的答案,所以我想把它贴出来,作为我未来的参考。
它来自http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/的链接
class Base
{
public:
int m_nPublic; // can be accessed by anybody
private:
int m_nPrivate; // can only be accessed by Base member functions (but not derived classes)
protected:
int m_nProtected; // can be accessed by Base member functions, or derived classes.
};
class Derived: public Base
{
public:
Derived()
{
// Derived's access to Base members is not influenced by the type of inheritance used,
// so the following is always true:
m_nPublic = 1; // allowed: can access public base members from derived class
m_nPrivate = 2; // not allowed: can not access private base members from derived class
m_nProtected = 3; // allowed: can access protected base members from derived class
}
};
int main()
{
Base cBase;
cBase.m_nPublic = 1; // allowed: can access public members from outside class
cBase.m_nPrivate = 2; // not allowed: can not access private members from outside class
cBase.m_nProtected = 3; // not allowed: can not access protected members from outside class
}
它本质上是派生类中基类的公共和受保护成员的访问保护。通过公共继承,派生类可以看到基类的公共成员和受保护成员。而私有继承则不能。使用protected,派生类及其派生的任何类都可以看到它们。