Jump to content
  • Advertisement
Sign in to follow this  
Tispe

Passing objects by reference or by pointer.

This topic is 2634 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!