Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ operator overloading += works but << doesn't work

I expected that += and << will work the same, but << somehow not working.

here is my code:

#include <iostream>

using namespace std;

struct Pos{
    int x;
    int y;

    void operator+=(Pos vel){
        x += vel.x;
        y += vel.y;
    }
};

struct Obj{
    string name;
    Pos pos;

    void info(){
        cout << name << endl;
        cout << pos.x << ", " << pos.y << endl;
        cout << endl;
    }
    void operator<<(Pos vel){
        pos += vel;
    }
    void operator+=(Pos vel){
        pos += vel;
    }
};


int main(){
    Pos p{10, 20};
    Obj car{"Car", p};
    Obj truck{"Big truck", {40, 20}};

    car.info();
    truck.info();

    //doesn't work
    car << {0, 10};
    //works
    car += {5, 10};
    //works
    car << Pos{0, 10};
    //works
    car += Pos{5, 10};

    car.info();
} 

most of them works but car << {0, 10};

It shows:

[Error] expected primary-expression before '{' token

I'm wondering what is the difference between += and << and why using constructor will work.

What am I missing here?

like image 756
Ben_TW Avatar asked Apr 28 '19 03:04

Ben_TW


People also ask

How do you overload an operator in C++?

Overloading an operator simply involves writing a function. C++ already does some operator overloading implicitly on built-in types. Consider the fact that the + operator already works for ints, floats, doubles, and chars. There is really a different version of the + operator for each type.

Does c Support Function overloading?

- GeeksforGeeks Does C support function overloading? First of all, what is function overloading? Function overloading is a feature of a programming language that allows one to have many functions with same name but with different signatures. This feature is present in most of the Object Oriented Languages such as C++ and Java.

What are the different types of operator overloading?

1 For operator overloading to work, at least one of the operands must be a user defined class object. 2 Assignment Operator: Compiler automatically creates a default assignment operator with every class. ... 3 Conversion Operator: We can also write conversion operators that can be used to convert one type to another type. ... More items...

What is the meaning of + operator in C++?

It is an idea of giving special meaning to an existing operator in C++ without changing their original meaning. Here, variables “a” and “b” are of type “int” and “float”, which are built in data types. Hence the addition operator ‘+’ can easily add the contents of “a” and “b”.


1 Answers

This: {10, 20} is a braced-init-list. It is not an expression. As such, it can appear only in specific pieces of C++ grammar.

For example, braced-init-lists can appear after a typename, which means they initialize a prvalue of that type. They can appear as an argument to a function. And (among several others) they can appear on the right-hand side of an assignment operator.

Note that += is an assignment operator.

<< is not one of these specific places. Therefore, a naked braced-init-list cannot appear on either side of a << expression. This is regardless of the fact that the << expression will be converted into a call to operator<< and thus the braced-init-list could be considered a function argument. C++ grammar simply doesn't allow a braced-init-list to appear there, so the compiler never gets far enough to even attempt overload resolution to figure out which function to call.

like image 91
Nicol Bolas Avatar answered Oct 08 '22 02:10

Nicol Bolas