可以在重载运算符中使用访问器方法吗?
因此,我一直被教导良好的编码实践是使用访问器方法而不是直接访问成员变量,但是在编写重载运算符时,如果在运算符类定义中使用这些访问器方法,我将无法编译。因此,假设有以下类:
class Point
{
public:
Point() {};
virtual ~Point() {};
// Accessor Methods
inline void SetX(ushort nX) { m_nX = nX; }
inline void SetY(ushort nY) { m_nY = nY; }
inline ushort GetX() { return m_nX; }
inline ushort GetY() { return m_nY; }
// Overloaded Operators
Point operator+(const Point& pnt);
private:
ushort m_nX, m_nY;
};
在运算符定义中,以下内容似乎完全合法,但它违背了我所学的内容:
Point Point::operator+(const Point& pnt)
{
Point myPoint;
myPoint.SetX(GetX() + pnt.m_nX);
myPoint.SetY(GetY() + pnt.m_nY);
return myPoint;
}
但是,以下内容编译时会出现错误:
Point.cpp:7:36: 错误:将“const Point {aka const Point}”作为“ushort Point::GetX()”的“this”参数传递会丢弃限定符 [-fpermissive]
Point.cpp:8:36:错误:将“const Point {aka const Point}”作为“ushort Point::GetY()”的“this”参数传递会丢弃限定符 [-fpermissive]
Point Point::operator+(const Point& pnt)
{
Point myPoint;
myPoint.SetX(GetX() + pnt.GetX()); // Here I am trying to use accessor methods vs. member variables
myPoint.SetY(GetY() + pnt.GetY());
return myPoint;
}
后一个代码如果从参数列表中删除“const”关键字,则会编译,我不完全理解,只是因为我传递了一个 const 变量,为什么这会消除我使用访问器方法的能力?
So I've always been taught that good coding practice is to use accessor methods versus directly accessing member variables, however while writing overloaded operators, I cannot compile if using these accessor methods within the operator class definition. So assume the following class:
class Point
{
public:
Point() {};
virtual ~Point() {};
// Accessor Methods
inline void SetX(ushort nX) { m_nX = nX; }
inline void SetY(ushort nY) { m_nY = nY; }
inline ushort GetX() { return m_nX; }
inline ushort GetY() { return m_nY; }
// Overloaded Operators
Point operator+(const Point& pnt);
private:
ushort m_nX, m_nY;
};
In the operator definition, the following seems perfectly legal, but it goes against what I've been taught:
Point Point::operator+(const Point& pnt)
{
Point myPoint;
myPoint.SetX(GetX() + pnt.m_nX);
myPoint.SetY(GetY() + pnt.m_nY);
return myPoint;
}
However, the following compiles with the error:
Point.cpp:7:36: error: passing 'const Point {aka const Point}' as 'this' argument of 'ushort Point::GetX()' discards qualifiers [-fpermissive]
Point.cpp:8:36: error: passing 'const Point {aka const Point}' as 'this' argument of 'ushort Point::GetY()' discards qualifiers [-fpermissive]
Point Point::operator+(const Point& pnt)
{
Point myPoint;
myPoint.SetX(GetX() + pnt.GetX()); // Here I am trying to use accessor methods vs. member variables
myPoint.SetY(GetY() + pnt.GetY());
return myPoint;
}
The latter code will compile if the 'const' keyword is removed from the parameter list, which I don't fully understand, just because I'm passing in a const variable, why does that eliminate my ability to use the accessor methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 getter 函数未标记为 const,因此无法在常量对象上调用:
如果没有 const 关键字,编译器必须假设该函数可能会修改对象,因此无法在常量上调用目的。值得注意的是,在某些情况下,您可能需要一个
const
和一个非const
版本,并具有不同的返回类型,例如:使用 getter 和 setter 是(在我看来)比直接访问右侧的成员更正确。
Your getter functions are not marked as const, so cannot be called on a constant object:
Without the
const
keyword, the compiler must assume that the function might modify the object, and thus cannot be called on a constant object. It's also good to note that in some cases, you may want both aconst
and a non-const
version, with different return types, such as:Using getters and setters is (in my opinion) more correct than accessing the right hand sides's members directly.
更改
为:
编译器抱怨正在尝试调用
const
对象上的non-const
方法。Change:
to:
The compiler is complaining that an attempt is being made to call a
non-const
method on aconst
object.