c++ - 为什么要调用复制构造函数?

标签 c++ copy-constructor

在下面的代码中,我创建了 4 个对象,分别命名为 p1p2p3copyplayer 类,我使用 while 循环打印它们的属性,其代码和输出如下。但我期待的是不同的输出,而且我不知道在前 3 种情况下我在哪里复制了对象。

#include <iostream>
using namespace std;
class player{
    public:
    int xp;
    string name;
    int health;

    player():player(0,0,"none") {} 
    player(int a):player(a,0,"none") {} 
    player (int a, int b, string c):name{c},xp{a},health{b} {}
    player (player &source)
    {
        name="copied player";
        xp=-1;
        health=-1;
    }
};
int main()
{
    player p1;
    player p2(2);
    player p3(2,5,"play3");
    player copy{p2};
    player arr[4]{p1,p2,p3,copy};
    int t=4;
    while(t--)
    {
        cout<<arr[3-t].name<<endl;
        cout<<arr[3-t].xp<<endl;
        cout<<arr[3-t].health<<endl;
    }
}

为此我得到以下输出:

copied player
-1
-1
copied player
-1
-1
copied player
-1
-1
copied player
-1
-1

然而,我期待:

none
0
0
none
2
0
play3
2
5
copied player
-1
-1

我不知道什么?

最佳答案

按照您的代码(以及评论中指出的),当您初始化 arr[4] 数组时,编译器将复制初始化程序中的每个对象列出目标 - 因此复制构造函数调用四次。

避免这种情况的一种方法是使用 std::move(x)在初始化列表中,但是,为此,您需要提供 move constructor对于您的 player 类(在您的情况下,默认值就足够了)。

但是,请记住,从对象移动后,源对象不再一定与原来相同,使用它可能无效。移动后的唯一要求(尽管类可能会提供更多保证)是对象处于可以安全销毁的状态。 (感谢来自 Jesper Juhl 的评论!)

此代码将产生您期望的输出:

#include <iostream>
#include <utility> // Defines std::move()
using std::string;
using std::cout; using std::endl;

class player {
public:
    int xp;
    string name;
    int health;

    player() :player(0, 0, "none") {}
    player(int a) :player(a, 0, "none") {}
    player(int a, int b, string c) :name{ c }, xp{ a }, health{ b } {}
    player(player& source) {
        name = "copied player";
        xp = -1;
        health = -1;
    }
    player(player&& p) = default; // Use the compiler-generated default move c'tor
};

int main()
{
    player p1;
    player p2(2);
    player p3(2, 5, "play3");
    player copy{ p2 };
//    player arr[4]{ p1,p2,p3,copy };
    player arr[4]{ std::move(p1), std::move(p2), std::move(p3), std::move(copy) };
    int t = 4;
    while (t--) {
        cout << arr[3 - t].name << endl;
        cout << arr[3 - t].xp << endl;
        cout << arr[3 - t].health << endl;
    }
    return 0;
}

注意:另请阅读:Why is "using namespace std;" considered bad practice? .

关于c++ - 为什么要调用复制构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61444685/

相关文章:

c++ - 在 C++ 中免费分析?

c++ - 如何将由矩形(边界框)裁剪的 voronoi 图转换为一组多边形?

c++ - 继承和复制构造函数——如何从基类初始化私有(private)字段?

c++ - 复制构造函数中的段错误

c++ - 为升压指针容器调用基类的复制构造函数?

c++ - 如何禁止复制构造函数并仅使用移动构造函数?

c++ - 继承将基类构造函数复制到派生类对象

c++ - 如何使用 C++ 从 RGB 缓冲区调整图像大小

c++ - Qt多线程启动线程报错

c++ - 这段代码有什么问题?它在第二个 cin 可以执行之前终止