In C++, what does & mean after a function's return type?

后端 未结 11 1498
情歌与酒
情歌与酒 2020-11-29 23:49

In a C++ function like this:

int& getNumber();

what does the & mean? Is it different from:

int getNumb         


        
11条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 00:24

    The first version allows you to write getNumber() = 42, which is probably not what you want. Returning references is very useful when overloading operator[] for your own containers types. It enables you to write container[9] = 42.

提交回复
热议问题