C++ 指针解引用问题
我有一个函数C++ 指针解引用问题,c++,pointers,swap,double-pointer,C++,Pointers,Swap,Double Pointer,我有一个函数void MOVE\u 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\u 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(…)
返回时,两个方块都指向接收方块,原本应该被移动的居住者完全消失了。想法/建议?我的代码被卡住了,直到我能解决这个问题。如果我在收到任何建议之前就解决了问题,我会回来回答我自己的问题。我假设您希望将实体的实例从sendingSquare移动到receivingSquare,并调整移动实体的CurrentSquar,使其与receivingSquare一致
如果是这种情况,下面的代码将执行此操作:
#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;
}
#包括
使用名称空间std;
班级广场;
类实体
{
公众:
平方*当前平方;
Entity():currentSq(NULL){}//使用构造函数初始化列表将currentSq设置为NULL
};
阶级广场
{
公众:
实体*占用人;
Square():占用者(NULL){}//使用构造函数初始化列表将占用者设置为NULL
};
//使用单个“*”将_移动到
无效移动到(正方形*发送正方形,正方形*接收正方形)
{
实体*movingOccupant=sendingSquare->occupant;
接收广场->占用者=移动占用者;
movingOccupant->currentSq=接收广场;
发送方->占用方=空;
}
int main(int argc,字符**argv){
//创建实例
Square*sendingSquare=new Square(),*receivingSquare=new Square();
实体*实体=新实体();
//相应地设置实例
发送方->占用方=实体;
实体->当前SQ=发送方;
//在将_移动到之前打印实例地址
//我们知道receivingSquare.occpuant.currentSq为空,printing receivingSquare.occpuant.currentSq被注释
你为什么使用Square**
指针而不是简单的Square*
指针作为MOVE\u TO
参数,这是完全不清楚的,但是如果做得好,它仍然可以工作。它应该以当前的形式工作。我在MOVE\u TO
中没有看到任何东西会产生你描述的奇怪行为。总之,调用MOVE_to
?如何调用?我会将MOVE_to
上的大小写更改为。它看起来像一个宏,因为宏通常都是大写的,所以它们作为宏突出显示。@AndreyT我将尝试使用单指针,看看它有什么作用。@Chris我用一组描述实体移动行为的枚举将它分组或者,我们可以看到调用MOVE_TO的周围代码,包括用于存储正方形和实体的变量的全部范围吗?