继承是否意味着内存的深层复制?

Me *_*d I -2 c++

我有一个类foo和bar.foo有资源.当bar从继承foo将它也有记忆的深层副本?或者我是否必须调用foofrom bardefault-constructor的构造函数?

class foo
{
    char *item;
    public:
        foo() : item(new char[5]) {}
};

class bar : public foo {};
Run Code Online (Sandbox Code Playgroud)

Alo*_*ave 5

派生类有:

All the members of base class + the additional members in derived class
Run Code Online (Sandbox Code Playgroud)

调用构造函数的顺序很明确,基类构造函数总是在派生构造函数之前调用.根据您使用成员初始化列表的方式,将调用默认基类构造函数或参数化版本.

但它始终是Base类构造函数,后跟Derived类构造函数.所以不确定你的第二个Q是什么.


而且确切地说,请删除该char *成员并使用std::string.

class foo
{
    //char *item;     ---------------->  Erroneous, difficult to handle
    std::string item;

    ....
Run Code Online (Sandbox Code Playgroud)