C++ compiler warning - returning local variable

前端 未结 3 1929
你的背包
你的背包 2020-12-09 17:40

I\'m simply trying to overload a + operator and I\'m getting this compiler warning

reference to local variable \'tmp\' returned

Here is the

3条回答
  •  旧巷少年郎
    2020-12-09 18:11

    tmp has a storage class of auto and will disappear when we exit. The answer is to specify static.

    static Int tmp = value + p.value;
    

    The storage assigned to tmp will remain reserved for the duration of the program.

提交回复
热议问题