提问人:Xan Nava 提问时间:1/1/2017 最后编辑:Xan Nava 更新时间:1/1/2017 访问量:976
如何正确调用返回常量引用的类成员的 setter
How do I properly call setters on a return constant referenced class member
问:
我的问题是我试图访问类成员的常量引用的无常量setter,从而导致错误(C2662)。如果我使设置的值可变,并且 setter 保持不变,那么我没问题,但是我已经读到您应该避免这样做,但找不到其他方法来做到这一点。
vector2D的定义:
class vector2D {
int _x;
int _y;
public:
vector2D() :
_x(0),
_y(0) {};
// Getters
int xGet() { return _x;};
int yGet() { return _y;};
// Setters
void xSet(int x) {
if (x > 100) {
_x = 100;
return;
} else if (x < 0) {
_x = 0;
return;
} else {
_x = x;
return;
}
};
void ySet(int y) {
if (y > 100) {
_y = 100;
return;
} else if (y < 0) {
_y = 0;
return;
} else {
_y = y;
return;
}
};
};
npc的定义:
class npc {
vector2D _position;
public:
npc () {};
const vector2D& positionGet() const{
return _position;
};
};
main.cpp :
main () {
vector2D myPos;
myPos.xSet(2); //Okay
npc myGuy;
myGuy.positionGet().xSet(2); //C2662
return 0;
}
我试过什么:
我尝试使 xSet/ySet 常量函数,但这给了我一个错误(expresion 必须是可修改的左值),这是有道理的。我一直在阅读有关此的文章,但正确的方法从未真正清楚。
我尝试使 x/y 可变,然后这样我就可以使 setter 函数恒定,这确实消除了错误,但后来我读到很多人说不要使用可变的,还有什么其他方法可以做到这一点?
我还试图使“_position”的返回值不变,但这样做不是不安全吗???
注意:我也在努力使我的问题变得更好,所以请留言/发表评论,说明我如何才能提出更好的:D
编辑:我发现了什么
因此,很多人建议只是返回一个非常量的“_position”引用,但我发现的问题是您可以直接为返回的引用赋值。
vector2D newPos;
myGuy.positionGet() = newPos;
这很糟糕,因为返回的值是私有成员,因此不应直接分配。 这也很糟糕,因为如果 npc 通过引用传递给函数,然后完成上述操作,一旦 vector2D 超出范围,它就会调用 vector2D 上的析构函数。
void functTest (npc & ch1) {
vector2D c1;
ch1.positionGet() = c1;
return;
}
出于某种原因,这也破坏了ch1._position ???
答:
如果你想让你的 getter 返回一个可变引用,那就这样做。
您想要调用并获取可以修改的对象。positionGet()
所以不要成功!const
vector2D& positionGet() {
return _position;
};
简单如。
评论
mutable
const
const
const
您可以为该位置提供可变接口和常量接口:
class npc {
vector2D _position;
public:
npc () {};
const vector2D& position() const { return _position; };
vector2D& mutable_position() { return _position; };
};
是的,当你调用 mutable_position() 时,将返回对私有成员的引用,但如果这是程序员的意图(如调用中明确使用“mutable_”所示),那么它应该没问题。
评论