3

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);
}
Community
  • 1
  • 1
Javier
  • 1,131
  • 4
  • 17
  • 22
  • 2
    Note that it is always a good idea to make member functions `const` if they don't need to change state. – Georg Fritzsche Nov 12 '10 at 13:11
  • Voted to close this as a duplicate. Basically, it is explained in [What is the meaning of a const at end of a member function?](http://stackoverflow.com/questions/4059932/what-is-the-meaning-of-a-const-at-end-of-a-member-function) (which I've now tagged as a [C++-FAQ](http://stackoverflow.com/questions/tagged/c%2b%2b-faq), so it will be easier to find). – sbi Nov 12 '10 at 13:21

8 Answers8

4

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.

Kos
  • 70,399
  • 25
  • 169
  • 233
2

+= 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).

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • But that's not why `getLength` has to be a const method. It's `refVector.getLength` that's the problem. – Peter Milley Nov 12 '10 at 13:10
  • @Peter Milley, I was getting to that. – Fred Foo Nov 12 '10 at 13:11
  • OK, thanks! BTW, if I should remove the first occurrence of "const" so as to have: Vector &Vector::operator+=(const Vector &refVector); why, I saw in some cases a const parameter when overloading the "operator =", if we need to modify the left-hand argument, e.g.: const Vector &Vector::operator =(const Vector & right) – Javier Nov 12 '10 at 13:25
  • AFAICT, that can't be correct, unless maybe one wants to prevent `x = y = z;` from working. – Fred Foo Nov 12 '10 at 13:30
2

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

Peter Milley
  • 2,768
  • 19
  • 18
1

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

Martin B
  • 23,670
  • 6
  • 53
  • 72
0

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.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
0

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.

ereOn
  • 53,676
  • 39
  • 161
  • 238
0

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.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
0

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.

Chowlett
  • 45,935
  • 20
  • 116
  • 150