简体   繁体   中英

How do I create a deep copy of a double pointer referring to a different class (C++)

as you may have figured I'm a bit new to the copy-construcor and so far I'm understanding the general concept of it but I have little to no idea about the syntax itself. I have a class called Yahtzee which is linked with two other classes, Protocol though a double pointer , and Dice though a dynamic array . My goal is to make a copy of Yahtzee through the copy-constructor but I'm having some trouble with how handle the double pointers.

Relevant code:

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);
};

So far I've had zero problems with the Dice class but when it comes to the syntax of playerprotocols I'm having quite some trouble. Everything that's // is my trails and errors.

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();
}

From my understanding and based on the error message no instance of constructor matches the argument list I figured my copy-constructor in Protocol lacks the proper syntax but I don't really know how to fix it.

Here the copy-constructor I'm refering to. I tried fixing it with * instead of & but that didnt not work and instead I got the error 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];
    }

I'm sure the answer is obvious but I can't quite put my finger on it. I would appreciate any help I could get.

EDIT** I added code for the constructors

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();
    }
}

and Protocol in case it's needed

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;
    }
}

You fixed the wrong thing, leave the Protocol copy constructor alone

this->playerProtocols[i] = new Protocol(object.playerProtocols[i]); //should work

should be

this->playerProtocols[i] = new Protocol(*(object.playerProtocols[i])); // will work

It's a double pointer, so to get to the actual object you need two dereferences, * and [i] .

Now it should be clear, pointers are hard work. The real lesson should be to rewrite your classes without raw pointers. Use smart pointers if you need to but avoid raw pointers. Mind you I can't see any reason in the posted code that you need pointers at all.

Without raw pointers, your copy constructor and assignment operator will literally write themselves .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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