0

我有一个函数void MOVE_TO(Square **sendingSquare, Square **receivingSquare)Square一个类在哪里:

class Square;
class Entity
{    
public:
    Square *currentSq;
};//  Just a data type--pretend it's your favorite class.
class Square
{
public:
    Entity *occupant;
};

void MOVE_TO(Square **sendingSquare, Square **receivingSquare)
{
    Entity *movingOccupant = (*sendingSquare)->occupant;
    (*receivingSquare)->occupant = movingOccupant;
    movingOccupant->currentSq = *receivingSquare;
    (*sendingSquare)->occupant = NULL;
}

问题是,当MOVE_TO(...)返回时,两个方格都指向接收方格,而应该被移动的居住者已经完全消失了。想法/建议?我的代码几乎被卡住了,直到我能解决这个问题。如果我在提出任何建议之前弄清楚我的问题,我会回来回答我自己的问题。

4

1 回答 1

0

我假设您要将实体的实例从发送方移动到接收方,并调整移动实体的 currentSq 以使其恢复为接收方。

如果是这种情况,下面的代码将执行此操作:

#include <iostream>
using namespace std;

class Square;
class Entity
{    
public:
    Square *currentSq;
    Entity(): currentSq(NULL){}//set currentSq to NULL using constructor initialization list
};

class Square
{
public:
    Entity *occupant;
    Square(): occupant(NULL){}//set occupant to NULL using constructor initialization list
};

//MOVE_TO with single '*'
void MOVE_TO(Square *sendingSquare, Square *receivingSquare)
{
    Entity *movingOccupant = sendingSquare->occupant;
    receivingSquare->occupant = movingOccupant;
    movingOccupant->currentSq = receivingSquare;
    sendingSquare->occupant = NULL;
}

int main(int argc, char** argv) {
    //create instances
    Square *sendingSquare = new Square(), *receivingSquare = new Square();
    Entity *entity = new Entity();

    //set up instances accordingly
    sendingSquare->occupant = entity;
    entity->currentSq = sendingSquare;

    //print instances address before MOVE_TO invoked
    //we know that receivingSquare.occupant is NULL, printing receivingSquare.occpuant.currentSq is commented
    cout << "sendingSquare: "<< sendingSquare 
         << ", sendingSquare.occupant: " << sendingSquare->occupant
         << ", sendingSquare.occupant.currentSq: " <<  sendingSquare->occupant->currentSq
         << ", receivingSquare: " <<receivingSquare 
         << ", receivingSquare.occupant: " << receivingSquare->occupant 
         //<< ", sendingSquare.occupant.currentSq: " << receivingSquare.occupant->currentSq
         << endl;

    MOVE_TO(sendingSquare,receivingSquare);

    //print instances address afer MOVE_TO invoked
    //we know that sendingSquare.occupant is NULL, printing sendingSquare.occpuant.currentSq is commented 
    cout << "sendingSquare: "<< sendingSquare 
         << ", sendingSquare.occupant: " << sendingSquare->occupant
         //<< ", sendingSquare.occupant.currentSq: " <<  sendingSquare.occupant->currentSq
         << ", receivingSquare: " << receivingSquare 
         << ", receivingSquare.occupant: " << receivingSquare->occupant 
         << ", receivingSquare.occupant.currentSq: " << receivingSquare->occupant->currentSq
         << endl;

    //commenting instance deletion. The program is ended anyway
    //delete entity,sendingSquare,receivingSquare;
    return 0;
}
于 2012-12-05T06:35:34.087 回答