Re: Real overload or not ?
On Jan 22, 11:20 am, Christophe Bourez <bou...@gmail.com> wrote:
Hi,
I am reviewing the coding guideline of an organisation and one of them
states that the name of the function members should start with an 'r'
if the function returns a non-const reference (I don't want to debate
whether returning a non-const ref is a good practice or not).
If you are reviewing then you are apparently asked for your opinion.
Why our opinion matters?
Here follows an example
class A
{
public:
int &rGetValue();
const int &GetValue() const;
...
};
Clearly i do not like const int&. I always pass int there.
Clearly, without the 'r', const and non const versions of GetValue
should have been considered as an overload . Prefixing the 'r' makes
obviously these member functions not real overload. Personally, I
really hate this kind of rule, but it is just a matter of taste. Could
you give me good reasons to prefer a real overload? For example, could
this kind of rule prevent the reuse of generic code, by the fact that
their names are not aligned?
Generics should assume to get non-const references from very few
places like operator=, operator* (unary) and operator[] these you can
not prefix with r anyway.
Any opinion is really welcome and thank you in advance for your
contribution.
My opinion is that passing non-const reference is bad practice unless
the class under question is container-like or pointer-like. With
container-like their rule is also bad since it causes a member named
'rat':
value_type const& at( size_type n ) const;
value_type& rat( size_type n );
As for pointer-like classes these usually overload the operators. For
navigating ( next(), prev(), up(), down(), left(), right() ) you pass
a pointer or pointer-like and not a reference since it is possible
that you are at edge and have to pass NULL. I am out of cases where i
need something that passes non-const reference with non-operator and
non-rat. ;)