Q:

Overriding +/-/*/% by integer and the -Weffc++ flag

I have a class called Coord...it has instance data x, y. I want to override the * operator such that the * operator can be used to multiply the Coord by an integer or double! This is the solution I put up:
Coord& Coord::operator*(int n)
{
    this->x = this->x*n;
    this->y = this->y*n;
    return *this;
}

It works - I've tested some print outs from main...the only issue is...I get a warning from the -Weffc++ flag! It says my function should return by value! I know this flag corresponds to the book "Effective C++" book, but I don't have a copy onhand - what does the book suggest? What is meant to pass by value?

A:

Just to elaborate on Grizzly's comment...  This is not the in-place multiplication.  As such, you should declare it const to explicitly prevent that:
Coord Coord::operator*(int n) const {
    Coord c(*this);
    c.x *= n;
    c.y *= n;
    return c;
}

Or if you have a useful constructor:
Coord Coord::operator*(int n) const {
    return Coord(x*n, y*n);
}

The in-place multiplication is different (and non-const):
Coord& Coord::operator*=(int n) {
    x *= n;
    y *= n;
    return *this;
}

