在编译时,当它返回添加对象的结果时,它sme error
就像使用删除函数一样。constexpr Player::Player(const Player&)
#include <bits/stdc++.h>
using namespace std;
class Player
{
char* name;
int num;
public:
Player(char* str = nullptr, int n = -1)
: name{str}
, num{n}
{
if (str != nullptr)
{
name = new char[strlen(str) + 1];
strcpy(name, str);
str = nullptr;
}
}
Player& operator=(const Player& temp)
{
delete[] this->name;
this->name = new char[strlen(temp.name) + 1];
strcpy(this->name, temp.name);
this->num = temp.num;
}
Player operator+(const Player& temp);
};
Player Player::operator+(const Player& temp)
{
char* str = new char[strlen(name) + strlen(temp.name) + 1];
strcpy(str, name);
strcat(str, temp.name);
int n = num + temp.num;
Player result{str, n};
delete[] str;
return result;
}
int main()
{
Player p1{"abc", 11};
Player p2{" xyz", 9};
Player p3;
p3 = p1 + p2;
}