Skip to main content
GameDev.net gamedev.net
🔒 Locked

References

Started by Simplicity Mar 24, 2006 at 7:48 PM 2 replies 400+ views
Original Post
Simplicity
Simplicity
When it comes to large structures it may make sence to pass parameters or return value by reference, but for data types of say char, short, long, int, etc, is it still good practice?
stylin
stylin
It's good practice if you need a reference to your POD types, yes. Otherwise, passing those by value is the common approach.
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
discman1028
discman1028
In general, pass by value for small #s of arguments. I believe the arguments will be put into registers if possible, which is faster.

So, by value:

data in RAM -> Register (1 RAM access)

By reference:

ptr to data in RAM -> Register (1 RAM access)
Inside func: data at the location of the ptr -> Register (1 more RAM access)
--== discman1028 ==--
RDragon1
RDragon1
Passing things like an int by reference isn't more efficient, if that's the question. The only reason you should pass by reference is if you actually want the function to modify the object passed in.

Also, remember that passing by non-const reference eliminates the possibility of being able to pass a parameter that can be implicitly converted to that type - that'll only work with by-value and by const ref.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.