Why does the size of a derived class include private members from the base class?

前端 未结 7 1724
谎友^
谎友^ 2021-02-09 19:52

I have the following code:

class A {
  private:
    int i;
 };

class B : public A {
 private:
  int j;
 };

When I check sizeof(B)

相关标签:
7条回答
  • 2021-02-09 20:03

    You either misunderstand sizeof or your misunderstand the layout (in memory) of C++ objects.

    For performance reason (to avoid the cost of indirection), compilers will often implement Derivation using Composition:

    // A
    +---+
    | i |
    +---+
    
    // B
    +---+---+
    | A | j |
    +---+---+
    

    Note that if private, B cannot peek in A even though it contains it.

    The sizeof operator will then return the size of B, including the necessary padding (for alignment correction) if any.

    If you want to learn more, I heartily recommend Inside the C++ Object Model by Stanley A. Lippman. While compiler dependent, many compilers do in fact use the same basic principles.

    0 讨论(0)
  • 2021-02-09 20:05

    i dont know what language this is, but when you inherit the original object still exists. that is why you can still call base.method()

    0 讨论(0)
  • 2021-02-09 20:14

    This has already been answered in a few other answers: access specifiers restrict access, but the member attributes of the class are still inherited.

    I just wanted to provide a rationale, as I usually learn better when I see the reasons for that. Basically, when you inherit from an type, the derived type contains a subobject of the base type, as small or large as the base might be. The reason for needing all of the member variables is that the derived object is a base object, and base level member functions can be called on it. Even if the derived type cannot access the private member attribute, the base methods that can be called on that object might still need to access it, so the members must be there:

    class base {
       int x;
    public:
       base() : x(0) {}
    // ...
       void printout() {
          std::cout << x << std::endl;
       }
    };
    class derived : public base {
    // ... assume that derived does not hide `printout`
    };
    int main() {
       derived d;
       d.printout();  // this requires access to d.base::x
    }
    

    This is only a simple example, and there are a few things that you can say here to argue that in some cases x can be made unneeded (we are overriding/hiding printout in the derived object...) but the language still allows you to access a hidden/overridden member method by qualifying, so d.base::printout() would still access printout at the base level, and that in turns requires x.

    0 讨论(0)
  • 2021-02-09 20:14

    Access specifiers (public/private/protected) don't affect inherited "object size" in anyway.

    0 讨论(0)
  • 2021-02-09 20:16

    It is inherited - the derived class object will contain it, but it can't be accessed by member functions of the derived class.

    0 讨论(0)
  • 2021-02-09 20:26

    all member variables are inherited. the private protected public modifiers only alter who has access to those variables

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