可以在重载运算符中使用访问器方法吗?

发布于 2025-01-04 07:47:15 字数 1457 浏览 0 评论 0原文

因此,我一直被教导良好的编码实践是使用访问器方法而不是直接访问成员变量,但是在编写重载运算符时,如果在运算符类定义中使用这些访问器方法,我将无法编译。因此,假设有以下类:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离笑几人歌 2025-01-11 07:47:15

您的 getter 函数未标记为 const,因此无法在常量对象上调用:

inline ushort GetX() const { return m_nX; }
                     ^^^^^

如果没有 const 关键字,编译器必须假设该函数可能会修改对象,因此无法在常量上调用目的。值得注意的是,在某些情况下,您可能需要一个 const 和一个非 const 版本,并具有不同的返回类型,例如:

const_iterator vector<T>::begin() const; //const version
iterator vector<T>::begin(); //mutable version

使用 getter 和 setter 是(在我看来)比直接访问右侧的成员更正确。

Your getter functions are not marked as const, so cannot be called on a constant object:

inline ushort GetX() const { return m_nX; }
                     ^^^^^

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 a const and a non-const version, with different return types, such as:

const_iterator vector<T>::begin() const; //const version
iterator vector<T>::begin(); //mutable version

Using getters and setters is (in my opinion) more correct than accessing the right hand sides's members directly.

壹場煙雨 2025-01-11 07:47:15

更改

inline ushort GetX() { return m_nX; }
inline ushort GetY() { return m_nY; }

为:

inline ushort GetX() const { return m_nX; }
inline ushort GetY() const { return m_nY; }

编译器抱怨正在尝试调用 const 对象上的 non-const 方法。

Change:

inline ushort GetX() { return m_nX; }
inline ushort GetY() { return m_nY; }

to:

inline ushort GetX() const { return m_nX; }
inline ushort GetY() const { return m_nY; }

The compiler is complaining that an attempt is being made to call a non-const method on a const object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文