Passing objects by reference or by pointer.

Started by
6 comments, last by ApochPiQ 12 years, 6 months ago
What is the preferred method and why?

Option 1:

void Render(DEVICEOBJECT &GraphicsDevice)
{
GraphicsDevice.StartRender();

// Render

GraphicsDevice.EndRender();
}


Option 2:

void Render(DEVICEOBJECT *GraphicsDevice)
{
GraphicsDevice->StartRender();

// Render

GraphicsDevice->EndRender();
}
Advertisement
Short answer: there is no preferred method.

There are lots of "schools" favoring one over the other. Sometimes people decide between the nullity behavior: A reference cannot (well, it can, but it's a rather uncommon way to happen) be null, whereas a pointer can be null. Also, you can assign to a pointer, but you cannot reassign a reference.

Other than that, the two are functionally equivalent.

Passing by reference allows the compiler to optimize better. Passing by pointer can lead to reloading of a pointer over and over. In general, there isn't a preferred method, like the poster above said. However, if you don't need a pointer, then it is a good habit to get into to pass by reference.
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.
A personal preference for readability is to always use a reference unless the passed value/object is going to be changed. As a result references are always const and when reading code like

doSomething(a, b, &c)

you immediately see that c is going to be modified by the function.

Of course the other approach mentioned above has advantages, too. It means whenever you see a function taking a pointer, null is going to be a valid value.
f@dzhttp://festini.device-zero.de
Use reference-to-const for read only parameter.
Use pointer for out parameter.
Use pointer-to-const for read only parameter when it allows to be NULL.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

It just comes down to style / convention, but:
Use reference-to-const for read only parameter.
Use pointer for out parameter.
Use pointer-to-const for read only parameter when it allowed to be NULL.
^^ In my experience, this is a very common coding style, which enhances readability as demonstrated by Trienco.

Also, if you were using option #2, it should include an assertion to validate the pointer before use:void Render(DEVICEOBJECT *GraphicsDevice)
{
assert( GraphicsDevice )
GraphicsDevice->StartRender();
Any parameter that is optional (can accept a NULL value) is a pointer.
Parameters that are required are references.

If it is an output, do not make it const, otherwise do, whether pointer or reference.

Exceptions are arrays, or pointers to buffers, which are always passed as pointers.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Our conventions at work are pretty simple:

  • Value when copies are cheap (mostly meaning intrinsic 32 or 64 bit types)
  • Const reference when copies are not cheap
  • Const pointer when optional (exception for const char*)
  • Pointer when mutable by the function itself
  • Conformant array for any buffer with a known size (optionally const), where the size is passed as an additional parameter


I like it.

My own style in personal projects generally uses references over pointers for mutables, but that's just a quirk. I also tend to prefer container classes to raw arrays, so I don't use the conformant array syntax. So on a few subtle points I don't think there's really a right or wrong answer, but there are some fundamentals to keep in mind:

  • Don't copy unless you need copy semantics (or if you're passing a small intrinsic type)
  • Be const-correct
  • Try to make it easy to tell what a function will do with a parameter just by reading its signature


IMHO if you can hit all three points, any style is legit.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement