Thumb rule for passing arguments

  • Thread starter Gonçalo Rodrigues
  • Start date
G

Gonçalo Rodrigues

Hi all,

I have a single-rooted hierarchy of heap-allocated objects -- call the
root Object. These objects are handled via a smart pointer template
Reference<T>. Basically, Reference<T> is a wrapper around T* with some
extra smarts including reference counting to make memory management
easier. Below, Reference<Object> will be denoted simply by Ref.

Since you only get your hands on Reference<T> and never on a T (or T*)
directly, my thumb rule has been, at least in the public interface of
the T class, to pass and return by Reference<T> (in most cases,
actually Ref). In addition, until now I have used

- return Reference<T> by const value, e.g.

const Reference<T> someMethod(...);

The const to avoid such perplexing oddities as doing:

someMethod(...) = ...;

My question is now, what would be your thumb-rule for passing
arguments? Two basic choices:

- By value, e.g. someMethod(Reference<T> someArg).

- By reference, e.g. someMethod(const Reference<T>& someArg)

Since Reference<T> is basically a pointer, I am inclined to
pass-by-value. This has the inconvenience of triggering a call to the
copy-constructor which implies the creation of another object and
fiddling of the reference count of T. On the other hand, there is one
less layer of indirection.

OTOH, looking at some Boost library smart pointer classes, they
usually use the pass-by-reference form.

Do you have any thumb-rule advice? Since I am writing this hierarchy
from scratch and for my own use (for now at least) I tend to value
consistency and doing everything the same way everywhere.

TIA, with my best regards,
G. Rodrigues
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top