使用inhertitance在C ++中销毁析构函数和成员变量的顺序是什么? [英] What order are destructors called and member variables destroyed in C++ using inhertitance?

查看:199
本文介绍了使用inhertitance在C ++中销毁析构函数和成员变量的顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常类似的问题,除了不完全相同:在C ++中调用析构函数和构造函数的顺序是什么
成员构造函数和析构函数调用的顺序

Very similar question as these, except not exactly: What is the order in which the destructors and the constructors are called in C++ Order of member constructor and destructor calls

我想知道:是成员吗?在调用基类的析构函数之前或之后销毁的派生类的变量?

I want to know: are the member variables of the derived class destroyed before or after the destructor of the base class is called?

这是使用Visual Studio 2008的C ++。谢谢。

This is in C++ using Visual Studio 2008. Thanks.

推荐答案

构造函数:一垒,然后派生

constructor: first base, then derived

销毁:


  • 〜衍生

  • 〜会员派生

  • ~base

  • 〜会员基础

  • ~derived
  • ~member derived
  • ~base
  • ~member base

代码:

class member {
    string s;

public:
    member(string s) {
        this-> s = s;
    }

    ~member() {
        cout << "~member " << s << endl;
    }
};

class base {
    member m;
public:
    base() : m("base"){
    }

    ~base() {
        cout << "~base" << endl;
    }
};

class derived : base{
     member m2;
public:

    derived() :m2("derived") {    }

    ~derived() {
        cout << "~derived" << endl;
    }
};

int main(int argc, const char * argv[]) {
    derived s;

    return 0;
}



参考文献&虚拟析构函数



当您计划动态分配时(即当您使用关键字 new & 删除)派生对象,然后始终具有虚拟受保护基础上的析构函数。在下面的示例中,动态删除基类引用上的对象会导致内存泄漏

References & virtual destructor

When you plan to dynamically allocate (i.e. when you use the keywords new & delete) a derived object, then always have a virtual or a protected destructor on your base. Dynamically deleting the object on the base class reference would otherwise lead to memory leaks in the example below:

class base {
    member m;
public:
    base() : m("base"){
    }

    /* correct behaviour is when you add **virtual** in front of the signature */
    ~base() {
        cout << "~base" << endl;
    }
};

class derived : public base{
     member m2;
    char* longArray;
public:

    derived() :m2("derived") {
        longArray = new char[1000];
    }


    ~derived() {
        delete[] longArray; // never called
        cout << "~derived" << endl;
    }
};

int main(int argc, const char * argv[]) {
    base *s = new derived; // mind the downcast to **base**

    delete s; /* only the non-virtual destructor on the base and its members is called. 
               No destructor on derived or its members is called.
               What happens to the memory allocated by derived?
               **longArray** is leaked forever. 
               Even without **longArray**, it most probably **leaks** memory, as C++ doesn't define its behaviour 
               */
    return 0;
}

输出:


  • ~base

  • 〜会员基础

仅基础数据已清理, longArray 泄露

Only base data is cleaned up, and longArray leaks.

这篇关于使用inhertitance在C ++中销毁析构函数和成员变量的顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆