Home > Blockchain >  How are objects from derived classes constructed in C
How are objects from derived classes constructed in C

Time:11-18

Im not a C pro, i've done mostly Java and C#. A teacher said something that confused me today. I've tried to validate the info by doing some research, but i ended up even more confused.

Lets say i have class A and class B. Class A is the base class and B is derived from A.

Now i already know that when an object of class B is made, the constructor from class A is called, and then the constructor from classe B is called. Similarly, when and object of class B is destroyed, the class B destructor is called, and then the ones from class A.

So far, my understanding was that Class B contains everything from class A except it's constructors and destructors. I thought that when an object from class B was built, only one object (of both types A and B) was created in memory.

Now my teacher is saying that when B is built, 2 separate objets are created and somehow "linked" together: one of class A and then one of class B. Both objects would exist in memory until the destruction of the class B object is called. Then the class B objet would be destroyed before the class A object is destroyed.

Which one is correct?

ps sorry if my english is so-so, it's not my native language...

Edit: ill try to rephrase:

I think: Class B contains all attributes and methods of class A. When i create an object from class B, only 1 object exists in memory. The constructor of class A is called just to initialize the part of my object that was originally from class A.

The teacher said: When i create an object from class B, 2 objects are created in memory. When i command the destruction of my object, the class B object that is in memory is destroyed first, and then the class A object that is also remaining in memory is destroyed. The teacher was never able to clarify how the class B object is able to use the methods and attributes of the class A object.

For me, this also seems to imply that there is a "ghost object" somewhere in memory that i am unaware of and of which i have almost no control.

CodePudding user response:

According to http://www.vishalchovatiya.com/memory-layout-of-cpp-object/#Layout_of_C_Object_With_Inheritance

These two classes:

class X {
    int     x;
    string str;
public:
    X() {}
    virtual ~X() {}
    virtual void printAll() {}
};
class Y : public X {
    int     y;
public:
    Y() {}
    ~Y() {}
    void printAll() {}
};

Would be represented in memory so that the memory layout of Y contains the data members of the base class followed by the data members of the derived class:

      |                              |          
      |------------------------------| <------ Y class object memory layout
      |          int X::x            |
stack |------------------------------|
  |   |              int string::len |
  |   |string X::str ----------------|
  |   |            char* string::str |         
 \|/  |------------------------------|      |-------|--------------------------|
      |           X::_vptr           |------|       |       type_info Y        |
      |------------------------------|              |--------------------------|
      |          int Y::y            |              |    address of Y::~Y()    |
      |------------------------------|              |--------------------------|
      |               o              |              | address of Y::printAll() |
      |               o              |              |--------------------------|
      |               o              |              
------|------------------------------|--------
      |           X::X()             | 
      |------------------------------|       |   
      |           X::~X()            |       |
      |------------------------------|       | 
      |         X::printAll()        |      \|/ 
      |------------------------------|  text segment
      |           Y::Y()             |
      |------------------------------|
      |           Y::~Y()            |
      |------------------------------|
      |         Y::printAll()        |
      |------------------------------|
      |      string::string()        |
      |------------------------------|
      |      string::~string()       |
      |------------------------------|
      |      string::length()        |
      |------------------------------|
      |               o              |
      |               o              |
      |               o              |
      |                              |

That same page also has memory layouts of other scenarios, including multiple inheritance and virtual inheritance.

  • Related