开发者

operator += overload, why const? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:开发者_高级运维

What is the meaning of a const at end of a member function?

Dear all,

I was trying to overload the operator += and I was getting some error of "discard qualifiers", only by adding "const" at the end of a method, I was able to get rid of the error. Does anybody could explain me why this is needed? Below, the code.

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);
}


The operator+= method receives its argument as a reference-to-constant, so it is not allowed to modify the state of the object it receives.

Therefore, through a reference-to-const (or pointer-to-const) you may only:

  • read the accessible fields in that object (not write),
  • call only the methods with the const qualifier (which indicates that this method is guaranteed not to modify the internal state of the object),
  • read or write accessible fields declared as mutable (which is very seldomly used and not relevant here).

Hope that helps.


+= modifies its left-hand argument, which is *this when you overload it for a class. Therefore, you can't make that argument const. Instead, use

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

That being said, because its right-hand argument has to be const (by definition), you can't call a non-const member function on the right-hand argument (refVector).


You're calling getLength on refVector, which is a const Vector &. You can only call const methods on a const reference.


operator+= takes a const reference to refVector; you're only allowed to call const methods on const references, so getLength() has to be const.


You are calling it like this refVector.getLength(), and refVector is declared const Vector & refVector, so getLength has to be declared as ok to call on a const.


Your implementation of operator+= takes a const Vector& onto which you call the method getLength().

However, you can't call getLength() on a const object if the object (or a reference to it) is const.

That's why you add to add const after getLength() declaration, so as to say that it is ok to call it even on const objects.


refVector is a const Vector&, so it can only call const member functions from Vector.

The const at the end of the method signifies that the method will not modify the internal state of this, and allows the method to be called be const objects.


In operator+=, refVector is a const parameter. This means that the function guarantees not to alter it, and any attempt to do so won't compile.

Adding the const keyword after a function's declaration is a guarantee that that function won't alter its receiver. In other words int getLength(); could, theoretically, alter refVector in refVector.getLength();. int getLength() const; is guaranteed not to. So the first won't compile, but the second will.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜