Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Passing objects by reference or by pointer.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Tispe   Members   -  Reputation: 1039

Like
0Likes
Like

Posted 26 September 2011 - 12:46 PM

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


Sponsor:

#2 clb   Members   -  Reputation: 1785

Like
0Likes
Like

Posted 26 September 2011 - 12:59 PM

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.


Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!

#3 smasherprog   Members   -  Reputation: 432

Like
0Likes
Like

Posted 26 September 2011 - 09:28 PM

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.

#4 Trienco   Crossbones+   -  Reputation: 2214

Like
2Likes
Like

Posted 26 September 2011 - 10:26 PM

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

#5 wqking   Members   -  Reputation: 756

Like
3Likes
Like

Posted 26 September 2011 - 11:04 PM

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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#6 Hodgman   Moderators   -  Reputation: 31112

Like
0Likes
Like

Posted 26 September 2011 - 11:13 PM

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


#7 L. Spiro   Crossbones+   -  Reputation: 14026

Like
0Likes
Like

Posted 26 September 2011 - 11:29 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#8 ApochPiQ   Moderators   -  Reputation: 16078

Like
2Likes
Like

Posted 26 September 2011 - 11:46 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS