繁体   English   中英

C ++中的对象创建和销毁顺序

[英]Object creation and destruction order in C++

我编写了一个简单的程序,以了解有关在C ++中创建和销毁对象的顺序的更多信息(使用Visual Studio 2015)。 这里是:

#include <iostream>
#include <string>

using namespace std;

class A
{
public:
    A(string name)
        : name(name)
    {
        cout << "A(" << name << ")::constructor()" << endl;
    }
    ~A()
    {
        cout << "A(" << name << ")::destructor()" << endl;
    }
private:
    string name;
};

class C
{
public:
    C(string name, A a)
        : name(name), a(a)
    {
        cout << "C(" << name << ")::constructor()" << endl;
    }
    ~C()
    {
        cout << "C(" << name << ")::destructor()" << endl;
    }
private:
    string name;
    A a;
};

class B
{
public:
    B(string name)
        : name(name)
    {
        cout << "B(" << name << ")::constructor()" << endl;
    }
    ~B()
    {
        cout << "B(" << name << ")::destructor()" << endl;
    }
private:
    string name;
    A a1{"a1"};
    A a2{"a2"};
    C c1{"c1", a1};
    A a3{"a3"};
};

int main()
{
    B b("b1");
    return 0;
}

输出使我有些惊讶( a1 s):

A(a1)::constructor()
A(a2)::constructor()
C(c1)::constructor()
A(a1)::destructor()
A(a3)::constructor()
B(b1)::constructor()
B(b1)::destructor()
A(a3)::destructor()
C(c1)::destructor()
A(a1)::destructor()
A(a2)::destructor()
A(a1)::destructor()

要了解发生了什么,我添加了有关对象实例的信息:

    A(string name)
        : name(name)
    {
        cout << "A(" << name << ")::constructor(), this = " << this << endl;
    }
    ~A()
    {
        cout << "A(" << name << ")::destructor(), this = " << this << endl;
    }

结果更加令人惊讶:

A(a1)::constructor(), this = 0039FB28
A(a2)::constructor(), this = 0039FB44
C(c1)::constructor()
A(a1)::destructor(), this = 0039F8A8
A(a3)::constructor(), this = 0039FB98
B(b1)::constructor()
B(b1)::destructor()
A(a3)::destructor(), this = 0039FB98
C(c1)::destructor()
A(a1)::destructor(), this = 0039FB7C
A(a2)::destructor(), this = 0039FB44
A(a1)::destructor(), this = 0039FB28

即,为什么a1的构造函数仅被调用一次,而析构函数仅被调用3次 我传递a按值,因此显然至少创建了一个临时对象,但是请向我解释何时以及创建和销毁了多少个 A实例?

正如注释中已经指出的那样,当您通过值将其作为参数传递时,也会通过复制构造来构造A类型A对象。 为了看到这一点,您可以自己添加一个复制构造函数:

A(const A& other)
: name(other.name)
{
    cout << "A(" << name << ")::copy-constructor(), this = " << this << endl;
}

样本输出:

A(a1)::constructor(), this = 0xbff3512c
A(a2)::constructor(), this = 0xbff35130
A(a1)::copy-constructor(), this = 0xbff350e8
A(a1)::copy-constructor(), this = 0xbff35138
C(c1)::constructor()
A(a1)::destructor(), this = 0xbff350e8
A(a3)::constructor(), this = 0xbff3513c
B(b1)::constructor()
B(b1)::destructor()
A(a3)::destructor(), this = 0xbff3513c
C(c1)::destructor()
A(a1)::destructor(), this = 0xbff35138
A(a2)::destructor(), this = 0xbff35130
A(a1)::destructor(), this = 0xbff3512c

在线尝试

如您所见,当您将a1作为参数传递给c1的构造函数时,将发生一种复制构造,而当此构造函数初始化其成员a时,将发生另一种复制构造。 此后,临时副本将在销毁后立即销毁,而在销毁c时销毁成员。

编辑:
在这里,您可以阅读创建复制构造函数时的确切规则。
为了不创建默认的复制构造函数,仅提供用户定义的构造函数是不够的,它必须是复制/移动构造函数。

编辑2:

取自C ++ 14标准(12.8复制和移动类对象):

7如果类定义未显式声明一个副本构造函数,则隐式声明一个副本构造函数。 如果类定义声明了move构造函数或move赋值运算符,则隐式声明的copy构造函数将定义为delete; 否则,将其定义为默认值(8.4)。 如果该类具有用户声明的副本分配运算符或用户声明的析构函数,则不建议使用后者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM