简体   繁体   English

C++ 复制构造函数和默认构造函数

[英]C++ copy Constructor and default constructor

i am learning c++ and have found a output which i dont really understand.我正在学习 C++ 并发现了一个我不太理解的输出。

#include <iostream>
using namespace std;

class A{
    public:
        A(){ cout << "A+" << endl;}
        A(const A&){ cout << "A(A)" << endl;}
        ~A(){cout << "A-" << endl;}
};

class B{
    public:
        B(){ cout << "B+" << endl;}
        B(const B&){cout << "B(B)" << endl;}
        ~B(){cout << "B-" << endl;}
    private:
        A a;
};

class C : public A{
    public:
        C(const B& b) : b1(b) { cout << "C+" << endl;}
        ~C(){ cout << "C-" << endl;}
    private:
        B b1,b2;
};

void test(A a){
    A m(a);
}

int main(){
    B b;
    C c(b);
    test(c);
    return 0;

}

the output is输出是

A+
B+
A+
A+
B(B)
A+
B+
C+
A(A)
A(A)
A-
A-
C-
B-
A-
B-
A-
A-
B-
A-

I mean the first one, B goes to default sees a we got a member from type A and goes to A thats the A+ than goes back to B and print B+ .我的意思是第一个,B 进入默认状态,看到我们从类型 A 中获得了一个成员,然后转到 A,即A+ ,然后返回到 B 并打印B+ Thats it with B b; B b; than C c(b) it goes to C, see its public A goes to A and print A+ than goes back see we got a Member B b1,b2 goes to B and B have a member A and goes agean to A and print A+ and than i dont understand why B(B) ?C c(b)它转到 C,看到它的公共 A 转到 A 并打印 A+ 然后返回看到我们有一个成员 B b1,b2 转到 B 并且 B 有一个成员 A,然后转到 A 并打印A+然后我不明白为什么是 B(B) ? after this B(B)i dont understand anything.. i try it to debugg but it didnt help me very much, maybe someone can explain why this works like this?在这个 B(B) 之后我什么都不明白..我尝试调试但它对我没有太大帮助,也许有人可以解释为什么会这样?

If I have understood correctly your question you are trying to understand the output如果我正确理解了您的问题,那么您正在尝试理解输出

A+
A+
B(B)
A+
B+
C+

that corresponds to this declaration对应于这个声明

C c(b);

The class C has base class A C类有基类A

class C : public A{

So the constructor of the class A is called所以A类的构造函数被调用

A+

then the data member b1 is created然后创建数据成员 b1

C(const B& b) : b1(b) { cout << "C+" << endl;}

The class B in turn has data member A类 B 又具有数据成员 A

class B{
    public:
        B(){ cout << "B+" << endl;}
        B(const B&){cout << "B(B)" << endl;}
        ~B(){cout << "B-" << endl;}
    private:
        A a;
};

So when the copy constructor of the class B is called the data member a is created所以当类 B 的拷贝构造函数被调用时,数据成员a被创建

A+
B(B)

The class C has one more data member of the class B. It is the data member b2.类 C 具有类 B 的另一个数据成员。它是数据成员 b2。 So these constructors are called所以这些构造函数被称为

A+
B+

And at last the body of the constructor C gets control最后,构造函数 C 的主体获得了控制权

C+

Destructors get the control in the reverse order relative to the order of creating objects.析构函数以与创建对象的顺序相反的顺序获得控制权。

So the destructors output of the object c looks the following way所以对象c的析构函数输出如下所示

C-
B-
A-
B-
A-
A-

You can make the program output more clear with minor changes of the program.您可以通过对程序的微小更改使程序输出更加清晰。

For example例如

#include <iostream>
using namespace std;

class A{
    public:
        A(){ cout << "A+" << endl;}
        A(const A&){ cout << "A(A)" << endl;}
        ~A(){cout << "A-" << endl;}
};

class B{
    public:
        B() : i( n++ ) { cout << "B+" << ' ' << i << endl;}
        B(const B&) : i( n++ ) {cout << "B(B)" << ' ' << i << endl;}
        ~B(){cout << "B-" << ' ' << i << endl;}
    private:
        size_t i;
        static size_t n;
        A a;
};

size_t B::n;

class C : public A{
    public:
        C(const B& b) : b1(b) { cout << "C+" << endl;}
        ~C(){ cout << "C-" << endl;}
    private:
        B b1,b2;
};

void test(A a){
    A m(a);
}

int main(){
    B b;

    std::cout << '\n';

    C c(b);

    std::cout << '\n';

    test(c);

    std::cout << '\n';
}

The program output of this updated program is这个更新程序的程序输出是

A+
B+ 0

A+
A+
B(B) 1
A+
B+ 2
C+

A(A)
A(A)
A-
A-

C-
B- 2
A-
B- 1
A-
A-
B- 0
A-

Lets take a closer look at the C constructor (slightly reformatted):让我们仔细看看C构造函数(稍微重新格式化):

C(const B& b)
    :
    b1(b)
{
    cout << "C+" << endl;
}

First the A constructor will be invoked, as it's a base-class for C .首先将调用A构造函数,因为它是C的基类。 That will print A+ .这将打印A+

Then the b1 member will be copy-constructed, which will print first A+ because of the B::a member, followed by B(B) in the B copy-constructor body.然后b1成员将被复制构造,由于B::a成员,它将首先打印A+ ,然后是B复制构造函数体中的B B(B)

Then the b2 member will be default constructed, which will print A+ (again because of the B::a member) followed by B+ .然后b2成员将被默认构造,它将打印A+ (再次因为B::a成员),然后是B+

Then the C constructor body will run which will print C+ .然后C构造函数体将运行,它将打印C+


The C constructor is really equivalent to this (with comments added): C构造函数实际上与此等效(添加了注释):

C(const B& b)
    : A(),      // Prints A+
      b1(b),    // Prints A+ and B(B)
      b2()      // Prints A+ and B+
{
    cout << "C+" << endl;    // Prints C+
}

Hopefully this will make it easier to see what's going on.希望这将使您更容易了解正在发生的事情。

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

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