Do Sub-Classes Really Inherit Private Member Variables?

前端 未结 7 932
醉酒成梦
醉酒成梦 2020-12-07 23:58

Basically as far as I know, when you create a base class with a public, protected, and private section and variables/functions in each the public and protected sections will

相关标签:
7条回答
  • 2020-12-08 00:04

    This may help

    #include<iostream>
    using namespace std;
    
    class A
    {
    int b;  
    };
    
    class B : private A
    {
    
    };
    
    int main()
    {
    C obj;
    cout<<sizeof(obj);  
    return 0;
    }
    
    0 讨论(0)
  • 2020-12-08 00:08

    You never call myObject.setMyVariable(), so myObject.getMyVariable() will not return 15.

    private does not imply static.

    0 讨论(0)
  • 2020-12-08 00:09

    "When you create a sub-class you never receive anything from the private section of the [base class]. If this is true then an object of the sub-class should never have it's own version of a private variable or function from the base class, correct?"

    No. The derived class inherits all the members of the base class, including the private ones. An object of the inherited class has those private members, but does not have direct access to them. It has access to public members of the base class that may have access to those members, but it (the derived class) may not have new member functions with such access:

    class yourClass : public myClass
    {
    public:
      void playByTheRules()
      {
        setMyVariable(); // perfectly legal, since setMyVariable() is public
      }
    
      void tamperWithMyVariable()
      {
        myVariable = 20; // this is illegal and will cause a compile-time error
      }
    };
    
    0 讨论(0)
  • 2020-12-08 00:11

    Basically as far as I know, when you create a base class with a public, protected, and private section and variables/functions in each the public and protected sections will get inherited into the appropriate section of the sub-class (defined by class subclass : private base, which will take all public and private members of base and put them into public, changing the word private to public puts them all in public and changing it to protected puts them all into protected).

    There's a bit of confusion in this statement.

    Recall that inheritance is defined for classes and structs in C++. Individual objects (ie. instances) do not inherit from other objects. Constructing an object using other objects is called composition.

    When a class inherits from another class, it gets everything from that class, but the access level of the inherited fields may inhibit their use within the inheritor.

    Furthermore, there are 3 kinds of inheritance for classes: private (which is the default), protected, and public. Each of them changes the access level of a class properties and methods when inherited by a subclass.

    If we order the access levels in this manner: public, protected, private, from the least protected to the most protected, then we can define the inheritance modifiers as raising the access levels of the inherited class fields to at least the level they designate, in the derived class (ie. the class inheriting).

    For instance, if class B inherits from class A with the protected inheritance modifier:

      class B : protected A { /* ... */ };
    

    then all the fields from A will have at least the protected level in B:

    • public fields become protected (public level is raised to protected),
    • protected fields stay protected (same access level, so no modification here),
    • private fields stay private (the access level is already above the modifier)
    0 讨论(0)
  • 2020-12-08 00:19

    myObject and yourObject are two different objects! Why should they share anything?

    Think about it that way: Forget about inheritance and suppose you have a class Person with private int age; and public void setAge (int age) {...}. You then instantiate two objects:

    Person bob;
    Person bill;
    bob.setAge(35);
    

    Would you expect Bill to be 35 now, too? You wouldn't, right? Similarly, your myObject doesn't share its data with yourObject.


    In response to your comment:

    The class yourClass inherits from myClass. That means that both yourObject and myObject have their own myVariable, the latter obviously by definition, the former inherited from myClass.

    0 讨论(0)
  • 2020-12-08 00:21

    After:

    class yourClass : public myClass {};
    

    there is still only one member variable. But there are two ways of accessing it by name: myClass::myVariable, and yourClass::myVariable.

    In these expressions, the class name is known as the naming class. The second key thing to understand is that access rights apply to the combination of naming class and member name; not just to the member name and not to the variable itself.

    If a member is mentioned without explicitly having the naming class present, then the naming class is inferred from the type of the expression on the left of the . or -> that named the member (with this-> being implied if there is no such expression).

    Furthermore, there are really four possible types of access: public, protected, private, and no access. You can't declare a member as having no access, but that situation arises when a private member is inherited.


    Applying all this theory to your example:

    • The name myClass::myVariable is private.
    • The name yourClass::myVariable is no access.

    To reiterate, there is only actually one variable, but it may be named in two different ways, and the access rights differ depending on which name is used.


    Finally, back to your original example. myObject and yourObject are different objects. I think what you intended to write, or what you are mentally imagining is actually this situation:

    yourClass yourObject;
    myClass& myObject = yourObject;
    //    ^^^
    

    which means myObject names the base class part of yourObject. Then after:

    yourObject.setMyVariable();
    

    the variable is set to 15, and so

    std::cout << myObject.getMyVariable() << std::endl;
    

    would output 15 because there is indeed only one variable.

    0 讨论(0)
提交回复
热议问题