简体   繁体   English

c++ 入门中关于复制构造函数的问题

[英]a question in c++ primer about copy constructor

Point global;

Point foo_bar(Point arg)
{
    Point local=arg,*heap=new Point(global);
    *heap=local;

    Point pa[4]={ local,*heap };
    return *heap;
}

the question is how many places are copy constructors used?问题是复制构造函数使用了多少个地方?

The supporting answer of this book says that * heap = local uses the copy constructor, but I don't think so.这本书的支持答案说 * heap = local 使用复制构造函数,但我不这么认为。 Isn't the copy assignment operator used here这里用的不是复制赋值运算符吗

Is there a problem with my understanding or is the answer wrong?是我的理解有问题还是答案有误?

To test this out, lets write a test program.为了测试这一点,让我们编写一个测试程序。

#include <iostream>

struct CopyConstructCheck {
    CopyConstructCheck() {}
    CopyConstructCheck(const CopyConstructCheck& other) { std::cout << "Copy constructed!\n"; }
    CopyConstructCheck(CopyConstructCheck&& other) noexcept { std::cout << "Move constructed!\n"; }

    CopyConstructCheck& operator=(const CopyConstructCheck& other) { std::cout << "Copy assigned!\n"; return *this; }
};

CopyConstructCheck global;
CopyConstructCheck* heap = nullptr;

CopyConstructCheck foo(CopyConstructCheck arg)
{
    CopyConstructCheck local = arg; heap = new CopyConstructCheck(global);
    *heap = local;

    CopyConstructCheck pa[2] = { local, *heap };
    return *heap;
}

int main()
{
    CopyConstructCheck ret = foo(CopyConstructCheck());

    delete heap;
    return 0;
}

Now when you run the above program, this is the output.现在当你运行上面的程序时,这是 output。

Copy constructed!
Copy constructed!
Copy assigned!
Copy constructed!
Copy constructed!
Copy constructed!

Now lets dissect this.现在让我们剖析一下。
The first copy construction happens here CopyConstructCheck local = arg;第一个复制构造发生在这里CopyConstructCheck local = arg; as your assigning the value from arg to the local variable upon instantiation.因为您在实例化时将值从arg分配给local变量。

The second is in heap = new CopyConstructCheck(global);第二个是在heap = new CopyConstructCheck(global); as your explicitly calling the copy constructor.作为您显式调用复制构造函数。

Third and forth copy constructions happen in this line CopyConstructCheck pa[2] = { local, *heap };第三次和第四次复制构造发生在这一行CopyConstructCheck pa[2] = { local, *heap }; as your initializing the array with existing data, thus calling the calling the copy constructor of each element.当您使用现有数据初始化数组时,因此调用调用每个元素的复制构造函数。

Finally the last happens in CopyConstructCheck ret = foo_return;最后最后发生在CopyConstructCheck ret = foo_return; line as your constructing the ret variable with the value returned by the function.行作为您使用 function 返回的值构造ret变量。 Note that I first assign the return to a reference variable so that we can handle delete the allocated memory block later.请注意,我首先将返回分配给引用变量,以便我们稍后可以处理删除分配的 memory 块。

Now after analyzing, we see that there has been an copy assignment happening right after the 2nd copy construction.现在经过分析,我们看到在第二次复制构建之后发生了复制分配。 If we trace back, its this line *heap = local;如果我们回溯,它的这一行*heap = local; . . And this proves that your hypothesis is true, it is indeed copy assignment.这证明你的假设是正确的,它确实是复制分配。 That's because your not constructing anything there, your assigning data to an existing variable.那是因为您没有在那里构建任何东西,而是将数据分配给现有变量。

Minor touches:轻微接触:
The sample code is the same as the OP's post but fixed a few errors like,示例代码与 OP 的帖子相同,但修复了一些错误,例如,

  1. Initializing an array with the size of 4 by using 2 variables in the initializer list.使用初始化列表中的 2 个变量初始化大小为 4 的数组。
  2. The heap allocated block was not deleted once everything is done.一切完成后,堆分配的块不会被删除。
  3. Dereferencing a pointer and then trying to assign a newly allocated pointer to it.取消引用一个指针,然后尝试为其分配一个新分配的指针。

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

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