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

Why did the copy constructor get called?

在下面的代码中,我创建了 4 个名为 p1p2p3copyplayer [=27] 对象=] 并使用 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] 数组时,编译器将 复制 初始化列表中的每个对象到目标 - 因此复制构造函数调用四次。

避免这种情况的一种方法是为您的 player class 使用 std::move(x) in the initializer list but, to do this, you will need to provide a move constructor(在您的情况下,默认值就足够了)。

但是,请记住,从对象移动后,源对象不再一定与原来相同,使用它可能无效。移动后的唯一要求(尽管 class 可能会提供更多保证)是对象处于可以安全销毁的状态。 (感谢 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?