C++ 为什么派生类继承基类的私有成员?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2016398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:50:14  来源:igfitidea点击:

why does the derived class inherit the private members of the base class?

c++

提问by skydoor

I know that the derived class can't access the private members of the base class, so why does the derived class inherit the private members of the base class? Is there any case that it is useful?

知道派生类不能访问基类的私有成员,那为什么派生类要继承基类的私有成员呢?有没有案例证明它有用?

Thanks!

谢谢!

回答by Richard Pennington

The derived class needs the private members even though it can't access them directly. Otherwise it's behavior would not build on the class it is deriving from.

派生类需要私有成员,即使它不能直接访问它们。否则它的行为不会建立在它派生的类上。

For example, pretend the private stuff is:

例如,假设私人的东西是:

int i;

and the class has a geti() and seti(). The value of i has to be put somewhere, even if it is private,

并且该类具有 geti() 和 seti()。i 的值必须放在某个地方,即使它是私有的,

回答by UncleO

The public and protected methods of the base class can still access private variables of the base class, and these methods are available in the derived class.

基类的public 和protected 方法仍然可以访问基类的private 变量,而这些方法在派生类中是可用的。

回答by John Dibling

The base class can still use the private member variables & methods.

基类仍然可以使用私有成员变量和方法。

If you want the derived classes to have access to members but keep those members hidden from the outside world, make them protected:.

如果您希望派生类可以访问成员但让这些成员对外界隐藏,请将它们设为protected:.

Here's an example to illustrate:

这里有一个例子来说明:

class Base
{
public:
  Base() : val(42.0f) {};
  float GetValue() const 
  {
    return val_;
  }
private:
  float val_;
};

class Derived : public Base
{
public:
  float ComputedValue() const
  {
    return GetValue() * 2.0f;
  } 
};

回答by Void

Don't forget that the base class may have methods that are not private, and thus accessible by the derived class. Those protectedor publicbase class methods can still invoke the privatebase class methods. This is particularly useful if you want to lock down core functionality in the base class, such as with a Template Methoddesign pattern implementation:

别忘了基类可能有一些方法不是private,因此可以被派生类访问。那些protectedpublic基类方法仍然可以调用private基类方法。如果您想锁定基类中的核心功能,例如使用模板方法设计模式实现,这将特别有用:

class base
{
public:

  virtual ~base() { /* ... */ }

  virtual void base_func() { foo_private (); }
  virtual void do_func() = 0;

private:

  void foo_private()
  {
    // pre-do_func() operations

    do_func();

    // post-do_function operations
  }

};

class derived : public base
{
public:

  void derived_func() { base_func(); }

  virtual void do_func()
  {
    // Derived class specific operations
  }
};

回答by Abhay

As has been outlined by other answers here, the derived class syntactically cannot access the private members of the base class; but it needs to have a copy of the same in its memory layout. Think of casting. using 'C' casting you can cast a derived to a private base. The compiler would then need the correct memory offset in order to produce a valid memory-layout for the base object.

正如此处的其他答案所概述的那样,派生类在语法上无法访问基类的私有成员;但它需要在其内存布局中有一个相同的副本。想想铸造。使用“C”转换,您可以将派生转换为私有基础。然后编译器需要正确的内存偏移量才能为基础对象生成有效的内存布局。

Ex.

前任。

class Base {
public: 
  void printA()  {
    a = 10;
    std::cout << a << std::endl;
  }
private:
  int a;
};
class Derived : private Base{
  int b;
};

Derived* d = new Derived;
Base* b = (Base*)d;
b->printA();

回答by Paul Nathan

The reason is because derived classes have an is-arelationship to the superclass.

原因是派生类is-a与超类有关系。

A derived class instantiation IS A superclass instantiation...just with more (or less due to setting some superclass functions private) stuff.

派生类实例化是超类实例化...只是具有更多(或由于将某些超类函数设置为私有而更少)的东西。

回答by Stefan Monov

The derived class doesn't "inherit" the private members of the base class in any way - it can't access them, so it doesn't "inherit" them.

派生类不会以任何方式“继承”基类的私有成员——它不能访问它们,所以它不会“继承”它们。

An instance of the derived class contains instances of the private members of the base class, for obvious reasons.

出于显而易见的原因,派生类的实例包含基类的私有成员的实例。

So I don't even know what you mean by that question.

所以我什至不知道你问这个问题是什么意思。

回答by ishan

when derived class object is created, base class constructor is also called for base object creation. if private members of base class are not allocated memory , the base object will be incomplete.

创建派生类对象时,还调用基类构造函数来创建基对象。如果基类的私有成员没有分配内存,基对象将是不完整的。

hence derived class object inherits private members of base, as they are created during creation of base class object, but are not accessible as they are private.

因此派生类对象继承基类的私有成员,因为它们是在创建基类对象期间创建的,但由于它们是私有的,因此不可访问。

回答by Parmanand Parihar

Although the private members are not accessible from the base class, they are inherited by them because these properties are used by the derived class with the help of non-private functions.

虽然私有成员不能从基类访问,但它们被它们继承,因为派生类在非私有函数的帮助下使用这些属性。

Private members of the base class are not directly accessed, but derived by base class by derived class.

基类的私有成员不是直接访问的,而是由基类派生的派生类。