1

The following code demonstrate simple operator overloading:

Class Position has 3 int field and a method to print them.

class Position
{

    private:
        int x,y,z;

    public:
        void print()
        {
            cout << x << "," << y << "," << z << endl;
        }

        Position (int a, int b, int c)
        : x(4),
        y(50),
        z(23)
        {
            x=a;
            y=b;
            z=c;
        }

        Position operator+(const Position &other);
        Position operator*=(const int &multi);
};

Operators + and *= are overloaded as such:

Position Position::operator+ (const Position &other)
{
    x = x + other.x;
    y = y + other.y;
    z = z + other.z;

    return *this;
}


Position Position::operator*= (const int &multi)
{
    x = x * multi;
    y = y * multi;
    z = z * multi;

    return *this;    
}

The code runs:

int main()
{
    Position p5( 1, 2, 3 );
    p5.print();

    Position p6( 4, 5, 6 );
    p6.print();

    Position p7 = p5 + p6;
    p7.print();

    p7 *= 2;
    p7.print();

    (p7 *= 2) *= 3;
    p7.print();
}

The result yielded are:

1,2,3
4,5,6
5,7,9
10,14,18
20,28,36

Question is why won't the last result perform as it should when it is done in nested?

8
  • 9
    Because you return by value, not by reference. Commented May 31, 2014 at 17:46
  • 1
    Because you do not return Position by reference from operator *=. Commented May 31, 2014 at 17:49
  • 1
    Your binary operator+ modifies the LHS operand. This is unlikely to be the desired behaviour. Commented May 31, 2014 at 17:49
  • 1
    See the Binary Arithmetic Operators section of this answer: Common operators to overload Commented May 31, 2014 at 17:52
  • 2
    The correct answer has already been given, but your constructor is also strange. It first initialises your members to some hard-coded values, then assigns the real ones. Why doesn't it initialise them to the real ones? Commented May 31, 2014 at 17:57

1 Answer 1

9
(p7 *= 2) *= 3;

won't work since you're assigning to a temporary, as you return by value in the operator*= function.

For the sub-expression

p7 *= 2

your operator function is called only for its side-effect, as the temporary it returns would be thrown away. To know more about temporaries, read What are C++ temporaries? and Temporary objects - when are they created, how do you recognise them in code?

When you want expression chaining, you've to return by reference like so

Position& Position::operator*= (int multi)
{
    x = x * multi;
    y = y * multi;
    z = z * multi;

    return *this;    
}

Aside: You don't have to pass built-in types by const reference(const int &multi), non-const copy (int multi) should do fine.

Sign up to request clarification or add additional context in comments.

2 Comments

You probably mean "primitive types", not "POD", if by "POD" you mean structs with only public member variables.
@ChristianHackl: Aah, right, thanks for bringing it up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.