У меня есть функция 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(...)
возвращается, оба квадрата затем указывают на принимающий квадрат, и тот, кто должен был быть перемещен, исчез совсем. Идея в / предложения? Мой код в значительной степени застрял, пока я не могу обойти это. Если я выясню свою проблему до того, как появятся какие-либо предложения, я вернусь и отвечу на свой вопрос.
Я предположил, что вы хотите переместить экземпляр сущности из sendSquare в receiveSquare и настроить 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;
}
Других решений пока нет …