在 C++ 中继承私有成员

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2676443/
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-28 00:19:08  来源:igfitidea点击:

Inheriting private members in C++

c++inheritance

提问by shreyasva

suppose a class has private data members but the setters and getters are in public scope. If you inherit from this class, you can still call those setters and getters -- enabling access to the private data members in the base class. How is this possible since it is mentioned that a derived class cannot inherit private data members

假设一个类具有私有数据成员,但 setter 和 getter 在公共范围内。如果您从这个类继承,您仍然可以调用这些 setter 和 getter —— 允许访问基类中的私有数据成员。这怎么可能,因为提到派生类不能继承私有数据成员

回答by Avi

A derived class doesn't inherit accessto private data members. However, it does inherit a full parent object, which contains any private members which that class declares.

派生类不继承对私有数据成员的访问。但是,它确实继承了一个完整的父对象,其中包含该类声明的任何私有成员。

回答by JRL

It depends on the inheritance type. If you inherit privately, then the derived class does NOT have access to the Base's private members.

这取决于继承类型。如果您私下继承,则派生类无权访问 Base 的私有成员。

Access                      public     protected    private
-----------------------------------------------------------
members of the same class      yes           yes        yes
members of derived classes     yes           yes         no
not members                    yes            no         no

回答by Billy ONeal

Because the getters and setters are public-- they're callable by anyone, not just derived classes.

因为 getter 和 setter 可以public被任何人调用,而不仅仅是派生类。

回答by mr.trovaz

you can access to them by set access to setters and getters public and acces to them like that

您可以通过设置对 setter 和 getter 的访问权限来访问它们,并像这样访问它们

*.h


class Mamifere
{
private:
    int a;
public:
    Mamifere();
    virtual ~Mamifere();
    int getA();
    // ~Mamifere(); //le delete dans le exp02() affiche seulement mamifere mort :( destructeur de la class mere 
    void manger() ;
    virtual void avancer() const;
};


class Deufin:public Mamifere{
public:
    Deufin();
    void manger() const;
    void avancer() const;
    ~Deufin();
};




*.cpp

Mamifere::Mamifere(){
        printf("nouveau mamifere est nee\n");
        this->a=6;
    }

Mamifere::~Mamifere(){
        printf("mamifere Mort :(\n");
    }
void Mamifere::manger() {
    printf("hhhh   je mange maifere %d\n",Mamifere::getA());
    }
void Mamifere::avancer() const{
    printf("allez-y Mamifere\n");
}

Deufin::Deufin(){
    printf("nouveau Deufin  est nee\n");
}
int Mamifere::getA(){
    return this->a;
}
void Deufin::manger() const{
    printf("hhhh   je mange poisson\n");

}
void Deufin::avancer() const{

    printf("allez-y Deufin\n");
}

Deufin::~Deufin(){
    printf("Deufin Mort :(\n");
}



main.cpp





void exp031(){
    Mamifere f;//nouveau mamifere est nee   //   nouveau Deufin  est nee
    Deufin d;

    f.avancer();//allez-y Deufin (resolution dynamique des lien  la presence de mot cle virtual)
    f.manger();//hhhh   je mange maifere (resolution static des lien pas de mot cle virtual)
    printf("a=%d\n",d.getA());//Deufin Mort :(   mamifere Mort :( (resolution static des lien  la presence de mot cle virtual) distructeur de class fille appel auromatiquement le destructeur de la class mere


}

int main(){
    exp031();

    getchar();
    return 0;
}

回答by Kotauskas

They are included, but not inherited. What this means is:

它们包括在内,但不是继承的。这意味着:

  • Any inheriting type (: public SomeClass, : protected SomeClassor even : SomeClass, equivalent to : private SomeClass) will notmake them accessible from child class methods, or outside (this->aand someobject.arespectively);
  • They will still be there - take space in memory allocated for child class instance;
  • Inherited methods will be able to access that private field.
  • 任何继承类型(: public SomeClass: protected SomeClass甚至: SomeClass等价于: private SomeClass都不会使它们可以从子类方法或外部(this->asomeobject.a分别)访问;
  • 它们仍然会在那里 - 占用为子类实例分配的内存空间;
  • 继承的方法将能够访问该私有字段。

So, basically protectedis not visible outside while visible inside and from derived classes (if : private Parentwasn't used), while privateis not visible from neither derived classes nor outside of the parent class; it's only visible for parent class' methods, even if they are inherited (but not overrided).

因此,基本上protected在外部不可见,而在派生类内部和派生类中可见(如果: private Parent未使用),而private派生类和父类外部都不可见;它仅对父类的方法可见,即使它们是继承的(但未被覆盖)。

回答by zmbush

Getters and setters do Not give you complete control over private data members. The control still lies with the base class.

Getter 和 setter 不会让您完全控制私有数据成员。控制权仍然在基类。

回答by Danvil

Using the pattern

使用模式

class MyClass {
  private: int a;
  public: void setA(int x) { a = x; }
  public: int getA() const { return a; }
};

seems object-orientated and has the sent of encapsulation.

似乎是面向对象的,并且有封装的发送。

Howeveras you noticed, you can still directly access the private field and there is nothing gained over just making apublic and accessing it directly.

但是,正如您所注意到的,您仍然可以直接访问私有字段,并且仅a公开并直接访问它没有任何好处。

Using getters and setters like this does not really make sense in C++.

像这样使用 getter 和 setter 在 C++ 中并没有什么意义。