【问题标题】:operator += overload, why const? [duplicate]运算符 += 重载,为什么是 const? [复制]
【发布时间】:2011-05-09 01:42:37
【问题描述】:

可能重复:
What is the meaning of a const at end of a member function?

亲爱的,

我试图重载运算符 += 并且我得到了一些“丢弃限定符”的错误,只有通过在方法末尾添加“const”,我才能摆脱错误。有人可以解释我为什么需要这样做吗?下面是代码。

class Vector{
    public:
        Vector();
        Vector(int);

        //int getLength();
        int getLength() const;
        const Vector & operator += (const Vector &);

        ~Vector();
    private:
        int m_nLength;
        int * m_pData;
};

/*int Vector::getLength(){
    return (this->m_nLength);
}*/

int Vector::getLength() const{
    return (this->m_nLength);
}

const Vector & Vector::operator += (const Vector & refVector){
    int newLength = this->getLength() + refVector.getLenth();
    ...
    ...
    return (*this);
}

【问题讨论】:

标签: c++ class operator-overloading constants


【解决方案1】:

operator+= 方法接收其参数作为对常量的引用,因此不允许修改其接收到的对象的状态。

因此,通过对 const 的引用(或指向 const 的指针),您只能:

  • 读取该对象中的可访问字段(而不是写入),
  • 只调用带有const限定符的方法(表示保证此方法不会修改对象的内部状态),
  • 读取或写入声明为 mutable 的可访问字段(很少使用,此处不相关)。

希望对您有所帮助。

【讨论】:

    【解决方案2】:

    operator+= 中,refVector 是一个const 参数。这意味着该函数保证不会更改它,并且任何这样做的尝试都不会编译。

    在函数声明后添加const 关键字可以保证该函数不会改变其接收者。换句话说,int getLength(); 理论上可以将refVector 更改为refVector.getLength();int getLength() const; 保证不会。所以第一个不会编译,但第二个会。

    【讨论】:

      【解决方案3】:

      refVectorconst Vector&,所以只能从Vector调用const成员函数。

      方法末尾的const表示该方法不会修改this的内部状态,允许调用的方法为const对象。

      【讨论】:

        【解决方案4】:

        您对operator+= 的实现采用const Vector&,您可以在其上调用方法getLength()

        但是,如果对象(或对它的引用)是 const,则不能在 const 对象上调用 getLength()

        这就是为什么你在 getLength() 声明之后添加 const 的原因,也就是说即使在 const 对象上也可以调用它。

        【讨论】:

          【解决方案5】:

          operator+= 接受对refVector 的常量引用;你只能在 const 引用上调用 const 方法,所以 getLength() 必须是 const。

          【讨论】:

            【解决方案6】:

            您这样称呼它refVector.getLength(),而refVector 被声明为const Vector & refVector,因此getLength 必须声明为可以调用const

            【讨论】:

              【解决方案7】:

              您在refVector 上调用getLength,这是一个const Vector &。您只能在 const 引用上调用 const 方法。

              【讨论】:

                【解决方案8】:

                += 修改其左侧参数,当您为一个类重载它时,它是 *this。因此,您不能提出该论点const。相反,使用

                Vector &Vector::operator+=(const Vector &refVector);
                

                话虽如此,因为它的右手参数必须是const(根据定义),所以不能在右手参数(refVector)上调用非const 成员函数。

                【讨论】:

                • 但这不是为什么 getLength 必须是 const 方法。 refVector.getLength 这就是问题所在。
                • @Peter Milley,我正要这么做。
                • 好的,谢谢!顺便说一句,如果我应该删除第一次出现的“const”以便拥有: Vector &Vector::operator+=(const Vector &refVector);为什么,在某些情况下,我在重载“operator =”时看到了一个 const 参数,如果我们需要修改左侧参数,例如: const Vector &Vector::operator =(const Vector & right)
                • AFAICT,这不可能是正确的,除非有人想阻止 x = y = z; 工作。
                猜你喜欢
                • 2014-04-22
                • 1970-01-01
                • 2015-06-23
                • 2018-03-16
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-09-23
                • 2011-02-07
                相关资源
                最近更新 更多