Implications of using an ampersand before a function name in C++?

后端 未结 5 1631
Happy的楠姐
Happy的楠姐 2020-12-17 17:38

Given the example:

inline string &GetLabel( ) {
        return m_Label;
};

Where m_Label is a private class member variable.

5条回答
  •  余生分开走
    2020-12-17 18:20

    You're are correct. It's a reference to the string member.

    The implication will be that if a caller were to assign a value or otherwise modify the returned string that they would also be modifying the member variable. If this is not the intent you may want to return a copy by value to avoid breaking encapsulation.

提交回复
热议问题