Re: The use of const reference instear of getter
On 2008-08-04 13:42, Pascal J. Bourguignon wrote:
Turin <omar.hamidi@gmail.com> writes:
Dear all;
As far as I understand the idea behind getter methods, it is used to
make sure that private memers of a class is returned appropriately to
the calling object.
However, if all I am interested in when making a member private is to
disallow the modification of the value of that member (read-only
member), then how about doing the following:
[...]
I would like to know the opinion of C++ experts on this and if there
are any side-effects of this.
Also from the perferformance point of view, isn't using this mor
effecient than using a getter?
Looking forwards to hearing your opinions.
class A {
public :
const int& ref_to_priv_member;
Instead, do this:
inline int ref_to_priv_member(void) const { return private_member; }
Nitpick:
inline is superfluous since the function is defined in the class
definition, and in C++ we usually write foo() and not foo(void), the
latter is considered bad style.
--
Erik Wikstr??m