Method call acting unexpectedly like an l-value

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

    Reasons for this behavior were described in other answers.

    One way to avoid this behavior would be qualifying your return type with const:

    struct FooStruct
    {
        ...
        const Foo GetBar() {return bar;}
    };
    

    You cannot assign values to const objects, so the compiler will complain.

提交回复
热议问题