Thursday, January 31, 2013

prefer pass by reference to const to pass by value

pass by reference is faster than pass by value.

Take a look at this example:

void Foo(const nameclass A) and void Fee(const nameclass &A);

when I call Foo, I invoke a call to A copy constructor. If the class is big enough and it is derived from something something something, it invokes the constructor of all of its parents.

 If I use reference, when I pass through  parameter, it doesnt invoke constructor call and doesnt reserve new memory. It just take A address.

So pass by reference is much faster and don't forget to set it to const

No comments:

Post a Comment