指针解除引用问题

时间:2012-12-05 04:08:00

标签: c++ pointers swap double-pointer

我有一个函数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(...)返回时,两个方格都指向接收方格,并且应该移动的占用者完全消失。理念' S /建议吗?我的代码几乎陷入困境,直到我可以解决这个问题。如果我在提出任何建议之前弄清楚我的问题,我会回来回答我自己的问题。

1 个答案:

答案 0 :(得分:0)

我假设您要将实体的实例从sendingSquare移动到receivingSquare,并调整已移动实体的currentSq以使其重新接收到receiveSquare。

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

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