PDA

View Full Version : passing class to a function in c++


the_real_remi
03-30-2005, 08:09 AM
Hi,
I'm writing a code that has to be as efficient as possible both in terms of memory use and execution speed. I'll have to pass a class instance (which is an 'intelligent' array) to a function (multiple times). Taking this into consideration, should I pass it as a pointer or reference? Is there any book or site that teaches stuff like this (optimizing code for performance)?
thanks for any help

playmesumch00ns
03-30-2005, 08:53 AM
The only rule is don't pass it by value!

Passing it by pointer and by reference should be exactly the same, since references are essentially just "safe" pointers that the compiler guards for you.

mummey
03-30-2005, 05:51 PM
The only rule is don't pass it by value!

Passing it by pointer and by reference should be exactly the same, since references are essentially just "safe" pointers that the compiler guards for you.

what he said. Never pass-by-value when you can avoid it.

dotTom
04-08-2005, 07:24 PM
what he said. Never pass-by-value when you can avoid it.

Not quite true :), if you need a local copy of the object within the function then passing by value rather than by pointer or reference (and then making a local copy) is more efficient. This is because a good optimizing compiler (such as Visual Studio 2003's C++ compiler) can detect this and optimize it out.

CGTalk Moderation
04-08-2005, 07:24 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.