Why can I expose private members when I return a reference from a public member function?

前端 未结 5 1763
[愿得一人]
[愿得一人] 2021-02-02 01:21

In the code snippet, I am able to access the private member variable outside the class scope. Though this should never be done, why is it allowed in this case? Is it a bad pract

5条回答
  •  一生所求
    2021-02-02 02:08

    Returning private members as reference is perfectly valid and the programmer who writes a class is responsible to carefully choose if this should be allowed. This link gives an example when this can be done.

提交回复
热议问题