-
pass by referrence
For clarity, when I pass by referrence like this:
Code:
class Something
{
public:
int i;
char c[20];
long l;
float f;
};
void DoSomething(Something &something);
the overhead is the same as if I passed a pointer like this:
Code:
void DoSomething(Something *something);
as opposed to if I pass by value like this:
Code:
void DoSomething(Something something);
in which case the overhead is sizeof(Something).
Is that all correct?
-
quote:
--------------------------------------------------------------------------------
C++ programmers strongly prefer references to pointers. References are cleaner and easier to use, and they do a better job of hiding information.
--------------------------------------------------------------------------------
quote:
--------------------------------------------------------------------------------
References cannot be reassigned, however. If you need to point first to one object and then another, you must use a pointer. References cannot be null, so if there is any chance that the object in question may be null, you must not use a reference. You must use a pointer
--------------------------------------------------------------------------------
this was borrowed from "teach yourself c++ in 21 days"
while passing by value is another story, as it copy the value to a new variable of the same type and let the function deal with the copy not with the original variable, so it doesnt affect the original variable you pass to it
i hope its clear for u :)
-
passing by value is sometimes bad for an struct, or class
1) if the object is big...OUCH! (inheritance..etc..)
2) it can all the default constructor forever..i mean forever!
that is all..
-
yes, passing by reference and passing by pointer have exactly the same overhead. they will do the same thing internally by the compiler.