我有一个看起来像这样的类:
class MeshClass
{
public:
Anchor getAnchorPoint(x, y)
{
return Anchor( this, x, y );
}
private:
points[x*y];
}
Run Code Online (Sandbox Code Playgroud)
我想创建另一个代表"锚"点的类,它可以访问Mesh并修改点,如下所示:
class Anchor
{
public:
Anchor(&MeshClass, x, y)
moveAnchor(x, y);
}
Run Code Online (Sandbox Code Playgroud)
问题是当我尝试Anchor在MeshClass::getAnchorPoint方法中做出类似的东西,return Anchor(this, x, y)但因为this是const我不能.作为一种解决方法,直到我弄明白这一点,我让Anchor接受对该点的引用,并且moveAnchor直接移动该点.
编辑:问题很可能是我尝试使用Reference时所做的事情.我改为使用像往常一样的指针,我可以传入this,没有来自编译器的抱怨.我几乎可以肯定我得到了一个与const相关的错误,但是我无法重新创建它,所以我必须忘记这一点.
Joe*_*ams 10
在C++中,这是一个指针,而不是引用.你可以这样做:
class Anchor; //forward declaration
class MeshClass
{
public:
Anchor getAnchorPoint(int x, int y)
{
return Anchor(*this, x, y );
}
private:
int points[WIDTH*HEIGHT];
}
class Anchor
{
public:
Anchor(MeshClass &mc, int x, int y);
}
Run Code Online (Sandbox Code Playgroud)