Method call acting unexpectedly like an l-value

前端 未结 4 1211
遥遥无期
遥遥无期 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:22

      f.GetBar() = m2;     // Here I'd expect an error such as
                           // "error: lvalue required as left operand of assignment"
    

    Your expectation is in error. This rule only applies to objects of built-in type.

    [C++14: 3.10/5]: An lvalue for an object is necessary in order to modify the object except that an rvalue of class type can also be used to modify its referent under certain circumstances. [ Example: a member function called for an object (9.3) can modify the object. —end example ]

    Recall that your = here is actually a call to operator=, which is a function.

提交回复
热议问题