returning reference to private vs public member

ⅰ亾dé卋堺 提交于 2019-12-04 16:05:59
Werner Erasmus

In general, returning a member by reference breaks as much encapsulation as having a public member, and neither is encouraged.

I suppose when a class is sufficiently simple (plain old data-it is anticipated that neither interface, nor data will ever change), one could make all its members public. Returning a non const reference had the same effect. All encapsulation is broken.

With respect to your question, there is no benefit.

In addition to my answer, similar answers have been given here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!