简体   繁体   English

复制构造函数在这段代码中是如何工作的?

[英]How does the copy constructor work in this code?

I have been reading Bruce Eckel's "Thinking in C++" and I came across the copy constructor.我一直在阅读 Bruce Eckel 的“Thinking in C++”并且遇到了复制构造函数。 While I understand mostly the need for a copy-constructor, I am a bit confused about the following code below:虽然我主要理解复制构造函数的必要性,但我对下面的代码有点困惑:

#include <iostream>
using namespace std;

class foo  
{
    static int objCount;
public:
    foo()
    {
        objCount++;
        cout<<"constructor :"<<foo::objCount<<endl;
    }
    ~foo()
    {
        objCount--;
        cout<<"destructor :"<<foo::objCount<<endl;
    }

};

int foo::objCount=0;
int main()
{
    foo x;
    foo y = x;
    return 0;
}

In the above code, constructor is called once and destructor twice.在上面的代码中,构造函数被调用一次,析构函数被调用两次。 Here's what I don't understand:这是我不明白的:

  1. y is an object of class foo , so why doesn't the compiler call the constructor and then copy the contents of x into y ? yclass foo的 object,那么为什么编译器不调用构造函数,然后将x的内容复制到y中呢?

  2. Where does the default copy-constructor provided by the compiler fit in this picture?编译器提供的默认复制构造函数适合这张图片的什么位置?

y is an object of class foo, so why doesn't the compiler call the constructor and then copy the contents of x into y? y 是 class foo 的 object,那么为什么编译器不调用构造函数,然后将 x 的内容复制到 y 中呢?

It does.确实如此。

It calls the copy constructor, to do exactly that.它调用复制构造函数来完成此操作。

Where does the default copy-constructor provided by the compiler fit in this picture?编译器提供的默认复制构造函数适合这张图片的什么位置?

You didn't provide your own copy constructor which produces any output, so the automatically-generated one (which doesn't) gets called.您没有提供自己的复制构造函数来生成任何 output,因此会调用自动生成的构造函数(不会)。

The default constructor (the one that looks like foo() ) doesn't get invoked at all during a copy.默认构造函数(看起来像foo()的构造函数)在复制过程中根本不会被调用。 Only the copy constructor (the one that looks like foo(const foo&) ).只有复制构造函数(看起来像foo(const foo&)的构造函数)。

Only one constructor happens per construction.每个构造只发生一个构造函数。 Well, unless you're using delegating constructors, but we'll talk about that another day.好吧,除非你使用委托构造函数,但我们改天再谈。


#include <iostream>
using namespace std;

class foo  
{
    static int objCount;
public:
    foo()
    {
        objCount++;
        cout<<"constructor :"<<foo::objCount<<endl;
    }
    foo(const foo&)
    {
        objCount++;
        cout<<"copy constructor :"<<foo::objCount<<endl;
    }
    ~foo()
    {
        objCount--;
        cout<<"destructor :"<<foo::objCount<<endl;
    }

};

int foo::objCount=0;

int main()
{
    foo x;
    foo y = x;
    return 0;
}

// constructor :1
// copy constructor :2
// destructor :1
// destructor :0
  1. y is an object of class foo, so why doesn't the compiler call the constructor and then copy the contents of x into y? y 是 class foo 的 object,那么为什么编译器不调用构造函数,然后将 x 的内容复制到 y 中呢?

In that case it calls not the constructor, but the copy constructor (which you didn't specified but compiler created it).在那种情况下,它不调用构造函数,而是调用复制构造函数(您没有指定但编译器创建了它)。 Here you create a new object y , so it needs to be constructed (copy-constructed because you use other object x ).这里新建了一个 object y ,所以需要构造(复制构造,因为你用了其他的 object x )。

foo y = x;
  1. Where does the default copy-constructor provided by the compiler fit in this picture?编译器提供的默认复制构造函数适合这张图片的什么位置?

You can also check this question.你也可以检查这个问题。 There's an explanation of other functions compiler creates implicitly.有对编译器隐式创建的其他函数的解释。 Sometimes it could be frustrating (especially when you work with raw pointers) so you should be aware of those functions.有时它可能会令人沮丧(尤其是当您使用原始指针时),因此您应该了解这些功能。

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

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