非静态类成员即使没有析构函数也会被销毁吗? [英] Are non-static class members destroyed even without a destructor?

查看:97
本文介绍了非静态类成员即使没有析构函数也会被销毁吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Bjarne Stroustrup的 C ++编程语言(第4版)第17.6节(生成默认操作)中提到了这一点:


如果程序员声明了某个类的复制操作,移动操作或
析构函数,则不会为该类生成任何复制操作,移动操作或
析构函数。


因此,我很困惑为什么在此程序中调用 SubObj 析构函数:

  #include< iostream> 
使用命名空间std;

class SubObj {
public:
〜SubObj(){
cout<< 称为SubObj的析构函数<<恩德尔
}
};

class Obj {
private:
SubObj so;

public:
Obj(){};
Obj(const Obj&o){};
};

int main(){
Obj();
cout<< 程序结束<<恩德尔
}

使用g ++编译时,我得到以下输出:

  $ ./a.out 
SubObj析构函数,称为
程序结束

根据我的理解,我希望不会自动生成 Obj 的默认析构函数,因为 Obj 的复制操作。因此,我希望 Obj SubObj 成员不会被销毁,因为没有<$ c的析构函数$ c> Obj 。



因此,我想知道:即使没有析构函数,对象成员也会自动销毁吗?还是为此示例自动生成了析构函数?



编辑:



这本书的后一部分(17.6.3.4)中,Bjarne在提到一个示例时提到:


我们定义了副本分配,所以我们还必须定义析构函数。
该析构函数可以是 = default ,因为它所要做的就是
确保成员 pos 已被删除,如果未定义副本分配,则无论如何将
完成。


在到目前为止的答案中,听起来好像Bjarne可能对此不对。

解决方案



当然如果以下情况仍会生成析构函数,您提供了一个复制构造函数。否则,将无法编译您的程序。



如果您提供自己的析构函数,则不会生成析构函数。不需要,也不能有两个。



此外,无论析构函数做什么,成员都将被销毁。析构函数允许您在对象(和子对象)生存期的常规规则之上执行额外工作。 SubObj 成员永远不会被销毁。


In Bjarne Stroustrup's "The C++ Programming Language (4th edition)" in section 17.6 (Generating Default Operations) it mentions this:

If the programmer declares a copy operation, a move operation, or a destructor for a class, no copy operation, move operation, or destructor is generated for that class.

Thus, I'm confused why the SubObj destructor is called in this program:

#include <iostream>
using namespace std;

class SubObj {
    public:
        ~SubObj() {
            cout << "SubObj Destructor called" << endl;
        }
};

class Obj {
    private:
        SubObj so;

    public:
        Obj() {};
        Obj(const Obj& o) {};
};

int main() {
    Obj();
    cout << "Program end" << endl;
}

When compiling with g++ I get the following output:

$ ./a.out
SubObj Destructor called
Program end

Based on my understanding, I expected the default destructor for Obj to not be auto-generated because I defined a copy operation for Obj. And thus, I expected that the SubObj member of Obj would not be destroyed because there is no destructor for Obj.

Thus, I'm wondering: are object members automatically destroyed even without a destructor? Or is a destructor somehow being auto-generated for this example?

Edit:

Later in the book (17.6.3.4), when referring to an example, Bjarne mentions:

We defined copy assignment, so we must also define the destructor. That destructor can be =default because all it needs to do is to ensure that the member pos is destyored, which is what would have been done anyway had the copy assignment not been defined.

Based on the answers so far, it sounds appears as though Bjarne may have just been wrong on this one.

解决方案

That phrase from the book is poorly worded/wrong.

Of course a destructor is still generated if you provide a copy constructor. If it weren't, your program would not be able to be compiled.

If you provide your own destructor, a destructor is not generated. It doesn't need to be, and you can't have two.

Also, members are destroyed regardless of what your destructor does. A destructor allows you to do "extra" stuff, on top of the normal rules for object (and subobject) lifetime. There was never a risk that the SubObj member wouldn't be destroyed.

这篇关于非静态类成员即使没有析构函数也会被销毁吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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