Why isn't operator overloading for pointers allowed to work?

后端 未结 9 2038
鱼传尺愫
鱼传尺愫 2020-12-03 14:08

As per the comment under this answer, references were introduced primarily to support operator overloading which quotes Bjarne Stroustrup:

References

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 15:00

    Why doesn't it work for pointers? Because it's ambiguous. Would

    ostream* operator<<(ostream* s, const char* c);
    

    match

    cout << 'a';
    

    or

    cout << "a";
    

    ?

    Also, you can't use address-of (&) with a temporary. What should this do:

    complex a, b, c;
    cout << a + b * c;
    

    since b * c is a temporary, and the sum is also.

    ?

提交回复
热议问题