Vector(const Vector& other) // Copy constructor
{
x = other.x;
y = other.y;
Why is the argument a const?
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.
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.