Saturday, June 16, 2007

prefer pass by reference to const to pass by value


default is pass by value.fn parameters are initialized with copied of actual parameters.

passing params by refernce avoids the slicing problem.
when a derived class object is passed as a base class object, then the base class constructor is called,and derived class parts are lost.... that means virtual functions will resolve to base class inside called function ?

i want to test it out in code.

for built in types, stl iterators and types for which you know that pass by value is inexpensive , function object types you can use pass by value ( though you wanna be sure that the size of these types wont become large in future).

what are function object types and why is pass by value appropriate for them ?
things to remember:
prefer pass by reference to const over pass by value. its typically more efficient and it avoids the slicing problem

the rule doesn't apply to built in types and stl iterators and function object types for them pass by value is usually appropriate




--
Regards,
Umesh Kumar

http://worthyarticles.blogspot.com/
http://learnbooks.blogspot.com/
http://cplusplusnotes.blogspot.com/

1 comment:

Dark Matter said...

I was searching for quick c++ revision material and got ur link. Its amazing and very nice of you to extract the core of a wonderful book in small paragraphs.

Hats off to you.