[openbeos] Re: Pointer or reference?

  • From: Ingo Weinhold <bonefish@xxxxxxxxxxxxxxx>
  • To: openbeos@xxxxxxxxxxxxx
  • Date: Sat, 27 Mar 2004 20:06:07 +0100

On 2004-03-27 at 19:32:32 [+0100], Waldemar Kornewald wrote:
> > > So, I would say, for public APIs (or those that may become public in the
> > > future), references should be avoided.
> > 
> > Sadly, I have to agree.
> 
> References have the advantage that the function/method already says it does 
> not accept NULL pointers. The data must always be defined.

True enough. Nevertheless they are sometimes not handy. E.g. references are 
what in Java would correspond to `final', i.e. they can't be changed after 
initialization.

> Additionally, when passing a BString object it is much better to pass the 
> reference because you cannot forget the &-sign and the code looks nicer:
> 
> BString string;
> ...
> control->SetLabel(string);
> versus
> control->SetLabel(&string);

By convention the Be API never expects strings to be passed in cases where 
the string is not changed by the callee (at least that is stated somewhere). 
Instead const char* is used. For functions that return a string via reference 
parameter, if there is a BString* version, then there's virtually always also 
a (char*, size_t) version.

> I really prefer the reference version. It could also state that the 
> SetLabel() method does not have side-effect (i.e.: it does not modify 
> string) which could be useful sometimes.

Huh? To ensure that something isn't changed, `const' is used. And it works 
for both, pointers and references.

CU, Ingo

Other related posts: