如何创建引用不同 class (C++) 的双指针的深层副本
How do I create a deep copy of a double pointer referring to a different class (C++)
正如您可能认为的那样,我对复制构造函数有点陌生,到目前为止我理解它的一般概念,但我对语法本身几乎一无所知。
我有一个名为 Yahtzee 的 class,它与另外两个 classes 链接,Protocol 通过双指针 ,Dice 通过动态数组链接。我的目标是通过复制构造函数制作 Yahtzee 的副本,但我在处理双指针方面遇到了一些麻烦。
相关代码:
class Yatzee {
private:
int nrOfPlayers;
int playerPos;
int currentPlayer;
Dice *dices[DICE]; //int DICE = 5
Protocol ** playerProtocols;
public:
Yatzee(int nrOfPlayers = 2);
Yatzee(const Yatzee &object);
//Yatzee& operator=(const Yatzee& object) const;
Yatzee& operator=(const Yatzee& object);
~Yatzee();
void addPlayer(string name);
};
到目前为止,我的 Dice class 问题为零,但是当涉及到 playerprotocols 的语法时,我遇到了一些麻烦。 // 的所有内容都是我的足迹和错误。
Yatzee::Yatzee(const Yatzee & object) {
nrOfPlayers = object.nrOfPlayers;
playerPos = object.playerPos;
currentPlayer = object.currentPlayer;
if (object.playerProtocols) {
playerProtocols = new Protocol*[object.nrOfPlayers];
for (int i = 0; i < object.nrOfPlayers; i++) {
//behöver skapa ny men behöver också samtidigt få med informationen från tidigare objectet
//playerProtocols[i] = new Protocol();
//använd copy constructor från Protocol här men hur?
//new Protocol(object.playerProtocols[i]);
//playerProtocols[i] = object.playerProtocols[i];
this->playerProtocols[i] = new Protocol(object.playerProtocols[i]); //should work
//new Protocol(playerProtocols[i], object.playerProtocols[i]);
//Protocol * playerProtocols[i] = new Protocol(object.playerProtocols[i]);
//new(&playerProtocols[i])Protocol(object.playerProtocols[i]);
//new(&dstObject) Object(&anotherObject);
//Object * obj = new Object(anotherObject); // not &anotherObject
//Object obj2(anotherObject);
//this->abc[i] = new ProtocolColumn(det andraobjektet.abc[i])
}
}
else {
playerProtocols = 0;
}
for (int i = 0; i < DICE; i++) {
//dices[i] = object.dices[i];
dices[i] = new Dice();
}
根据我的理解并基于错误消息没有构造函数的实例与参数列表匹配我认为我在协议中的复制构造函数缺少正确的语法,但我真的不知道知道如何修复它。
这里指的是复制构造函数。我尝试用 * 而不是 & 修复它,但这没有用,而是我得到了错误 cdcdcdcd。
Protocol::Protocol(const Protocol &oldProtocol) {
name = oldProtocol.name;
totalSum = oldProtocol.totalSum;
for (int i = 0; i < SIZE; i++) {
scoreboard[i] = oldProtocol.scoreboard[i];
spotTaken[i] = oldProtocol.spotTaken[i];
}
}
Protocol::Protocol(const Protocol *object){
name = object->name;
totalSum = object->totalSum;
for (int i = 0; i < SIZE; i++) {
scoreboard[i] = object->scoreboard[i];
spotTaken[i] = object->spotTaken[i];
}
我确定答案很明显,但我不能完全确定。如果能得到任何帮助,我将不胜感激。
编辑**我为构造函数添加了代码
Yatzee::Yatzee(int nrOfPlayers) {
this->nrOfPlayers = nrOfPlayers;
this->playerPos = 0;
this->currentPlayer = 0;
this->playerProtocols = new Protocol*[nrOfPlayers];
for (int i = 0; i < DICE; i++) {
this->dices[i] = new Dice();
}
}
和协议以备不时之需
class Protocol {
private:
string name;
int totalSum;
int scoreboard[SIZE];
bool spotTaken[SIZE];
public:
explicit Protocol(string name = "?");
Protocol(const Protocol &object);
Protocol& operator=(const Protocol& object);
~Protocol();
};
Protocol::Protocol(string name) {
this->name = name;
this->totalSum = 0;
for (int i = 0; i < SIZE; i++) {
this->scoreboard[i] = 0;
this->spotTaken[i] = false;
}
}
你修复了错误的东西,不要管 Protocol
复制构造函数
this->playerProtocols[i] = new Protocol(object.playerProtocols[i]); //should work
应该是
this->playerProtocols[i] = new Protocol(*(object.playerProtocols[i])); // will work
它是一个双指针,所以要到达实际对象,您需要两次取消引用,*
和 [i]
。
现在应该清楚了,指点是辛苦的。真正的教训应该是在没有原始指针的情况下重写 类。如果需要,请使用智能指针,但要避免使用原始指针。请注意,我在发布的代码中看不出有任何理由需要您的指导。
如果没有原始指针,您的复制构造函数和赋值运算符将字面上写自己。
正如您可能认为的那样,我对复制构造函数有点陌生,到目前为止我理解它的一般概念,但我对语法本身几乎一无所知。 我有一个名为 Yahtzee 的 class,它与另外两个 classes 链接,Protocol 通过双指针 ,Dice 通过动态数组链接。我的目标是通过复制构造函数制作 Yahtzee 的副本,但我在处理双指针方面遇到了一些麻烦。
相关代码:
class Yatzee {
private:
int nrOfPlayers;
int playerPos;
int currentPlayer;
Dice *dices[DICE]; //int DICE = 5
Protocol ** playerProtocols;
public:
Yatzee(int nrOfPlayers = 2);
Yatzee(const Yatzee &object);
//Yatzee& operator=(const Yatzee& object) const;
Yatzee& operator=(const Yatzee& object);
~Yatzee();
void addPlayer(string name);
};
到目前为止,我的 Dice class 问题为零,但是当涉及到 playerprotocols 的语法时,我遇到了一些麻烦。 // 的所有内容都是我的足迹和错误。
Yatzee::Yatzee(const Yatzee & object) {
nrOfPlayers = object.nrOfPlayers;
playerPos = object.playerPos;
currentPlayer = object.currentPlayer;
if (object.playerProtocols) {
playerProtocols = new Protocol*[object.nrOfPlayers];
for (int i = 0; i < object.nrOfPlayers; i++) {
//behöver skapa ny men behöver också samtidigt få med informationen från tidigare objectet
//playerProtocols[i] = new Protocol();
//använd copy constructor från Protocol här men hur?
//new Protocol(object.playerProtocols[i]);
//playerProtocols[i] = object.playerProtocols[i];
this->playerProtocols[i] = new Protocol(object.playerProtocols[i]); //should work
//new Protocol(playerProtocols[i], object.playerProtocols[i]);
//Protocol * playerProtocols[i] = new Protocol(object.playerProtocols[i]);
//new(&playerProtocols[i])Protocol(object.playerProtocols[i]);
//new(&dstObject) Object(&anotherObject);
//Object * obj = new Object(anotherObject); // not &anotherObject
//Object obj2(anotherObject);
//this->abc[i] = new ProtocolColumn(det andraobjektet.abc[i])
}
}
else {
playerProtocols = 0;
}
for (int i = 0; i < DICE; i++) {
//dices[i] = object.dices[i];
dices[i] = new Dice();
}
根据我的理解并基于错误消息没有构造函数的实例与参数列表匹配我认为我在协议中的复制构造函数缺少正确的语法,但我真的不知道知道如何修复它。
这里指的是复制构造函数。我尝试用 * 而不是 & 修复它,但这没有用,而是我得到了错误 cdcdcdcd。
Protocol::Protocol(const Protocol &oldProtocol) {
name = oldProtocol.name;
totalSum = oldProtocol.totalSum;
for (int i = 0; i < SIZE; i++) {
scoreboard[i] = oldProtocol.scoreboard[i];
spotTaken[i] = oldProtocol.spotTaken[i];
}
}
Protocol::Protocol(const Protocol *object){
name = object->name;
totalSum = object->totalSum;
for (int i = 0; i < SIZE; i++) {
scoreboard[i] = object->scoreboard[i];
spotTaken[i] = object->spotTaken[i];
}
我确定答案很明显,但我不能完全确定。如果能得到任何帮助,我将不胜感激。
编辑**我为构造函数添加了代码
Yatzee::Yatzee(int nrOfPlayers) {
this->nrOfPlayers = nrOfPlayers;
this->playerPos = 0;
this->currentPlayer = 0;
this->playerProtocols = new Protocol*[nrOfPlayers];
for (int i = 0; i < DICE; i++) {
this->dices[i] = new Dice();
}
}
和协议以备不时之需
class Protocol {
private:
string name;
int totalSum;
int scoreboard[SIZE];
bool spotTaken[SIZE];
public:
explicit Protocol(string name = "?");
Protocol(const Protocol &object);
Protocol& operator=(const Protocol& object);
~Protocol();
};
Protocol::Protocol(string name) {
this->name = name;
this->totalSum = 0;
for (int i = 0; i < SIZE; i++) {
this->scoreboard[i] = 0;
this->spotTaken[i] = false;
}
}
你修复了错误的东西,不要管 Protocol
复制构造函数
this->playerProtocols[i] = new Protocol(object.playerProtocols[i]); //should work
应该是
this->playerProtocols[i] = new Protocol(*(object.playerProtocols[i])); // will work
它是一个双指针,所以要到达实际对象,您需要两次取消引用,*
和 [i]
。
现在应该清楚了,指点是辛苦的。真正的教训应该是在没有原始指针的情况下重写 类。如果需要,请使用智能指针,但要避免使用原始指针。请注意,我在发布的代码中看不出有任何理由需要您的指导。
如果没有原始指针,您的复制构造函数和赋值运算符将字面上写自己。