views:

488

answers:

7
 Vector(const Vector& other) // Copy constructor 
 {
    x = other.x;
    y = other.y;

Why is the argument a const?

+15  A: 

Because you are not going to modify the argument other inside the copy ctor as it is const.

When you did x = other.x it essentially means this->x = other.x. So you are modifying only this object just by copying the values from other variable. Since the other variable is read-only here, it is passed as a const-ref.

Naveen
It's considered "good defensive programming" by (a) telling the caller that they don't have to worry about changes to the argument being passed in and (b) telling the implementor that they are not allowed to change the argument.
Mark
It's also necessary because you want to be able to bind rvalues to the ctor's argument: `X x = f();`. This isn't possible with non-const references.
sbi
A: 

The idea of a copy constructor is that you are copying the contents of the other object into the this object. The const is there to ensure that you don't modify the other object.

Graeme Perrow
+1  A: 

In order to not be able to change other (by accident)?

jensgram
A: 

Its not specific to copy constructor. In any function if you are not going to modify the internal state of the object then object will be passed as const.

Vector(const Vector& other) 
{
     //Since other is const, only public data member and public methods which are `const` can be accessed.
}
aJ
Since `Vector` is a `Vector`, can't you can access private members of `other`?. If you replaced the `Vector` argument with `OtherClass`, you can't access private members.
jamuraa
@jamuraa: You can access them, but you are not allowed to modify them. To attempt to is a compile-time error.
sbi
+17  A: 

You've gotten answers that mention ensuring that the ctor can't change what's being copied -- and they're right, putting the const there does have that effect.

More important, however, is that a temporary object cannot bind to a non-const reference. The copy ctor must take a reference to a const object to be able to make copies of temporary objects.

Jerry Coffin
And not only temporary objects - it also allows the copy constructor to be used on named objects that happen to be marked const.
Michael Burr
A: 

It can also come handy if you want to copy an object you only have a const reference to for example

...
const Vector& getPosition();
...

Vector* v = new Vector(getPosition());

If it wasn't for Vector(const Vector& other) that example would create a syntax error.

JustMaximumPower
+2  A: 

The traditional copy-ctor and friends take a const& parameter for reasons specified above. However, you should also look up move-semantics and r-value references (to be part of C++0x, if all goes well) to see why and when you will use copy-ctors without a const& parameter. Another place to look at is the implementation of smart pointers such as auto_ptr (which have transfer of ownership semantics) where non-const parameters are useful.

dirkgently