Method call acting unexpectedly like an l-value

前端 未结 4 1290
遥遥无期
遥遥无期 2021-01-17 17:56

Can anybody explain why this code compiles:

typedef struct longlong
{
  unsigned long low;
  long high;
}
longlong;

typedef longlong Foo;    

struct FooStr         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-17 18:21

    Using operators on objects of class type means to call a function (either a member function of the left-hand operand, or a free function taking the left-hand operand as first argument). This is known as operator overloading.

    It's fine to call functions on rvalues so there is no compilation error.

    When implementing the overloaded assignment operator, you can mark it so that it can not be called on an rvalue, but the designer of whatever class you are using chose not to do that.

提交回复
热议问题