开发者

when to pass by reference and when by value in c / c++ [duplicate]

This question already has answers here: 开发者_如何转开发 Closed 11 years ago.

Possible Duplicate:

Is it better in C++ to pass by value or pass by constant reference?

When to pass by reference and when to pass by pointer in C++?

I am fairly new to c and c++ and I have the following question with regards to passing parameters to functions and thought it best to ask here and come straight to the source of the knowledge from the gurus.

When should I pass parameters by value and when sould they be passed by reference? And what is the most commonly used method in the real world?

Thanks in advance.


Prefer pass by reference, usually one or more of:

  1. If it's big. (Definitely do this if it's const, do it anyway quite often though, depending on the rest of this list)
  2. If you want to edit it and make changes globally visible.
  3. If you want virtual functions to behave polymorphicly.
  4. In template code if you don't know what type(s) it's going to be.

Pass by value:

  1. If it's a trivial builtin type (e.g. int)
  2. If you don't want changes to be alter the original object.
  3. If you want to be able to pass temporaries in without having to make it const.


If the parameter is bigger than register and you're not modifying it, you should pass it as const reference. Of course, there are exceptional cases where you later create a copy or so, but in general this rule holds true.

For smaller types, it should not matter (i.e. const int& or int) should give the same result. To be safe, pass them by value anyway. For generic code, you can use the Boost Call Traits library which automatically decides whether to use by-reference or by-value.

If you want to modify the object, passing it in as reference allows you to modify it in-place, which is usually faster than returning a new one and overwriting the original one.


By default, pass by const reference, except:

1) If you want an in/out or out parameter - one that gets changed by the function. In that case, use a non-const reference. 2) If you are passing an integer, float or pointer - pass by value.


Generally, you should use pass by value when you need to change an argument, and don't want the caller to have to make copies. You should use pass by reference to const pretty much everywhere else.

However, this really comes down to the "C++ as a federation of languages" bit -- for example, if you're writing STL algorithms you need to use pass by value, because iterators and functors approximate pointers and function pointers.


It depends ,among other things,largely upon your needs.If you want to see the changes done to passed variable reflected in your current function,surely you will go with pass by reference but if you are strict that any changes to done to the passed variable is not reflected in your current function go for 'passed by value'.

Thanks, Mawia


Pass by value unless you have a reason not to. Reasons not to:

  1. You want the function to change the object in some way. Pass by reference or by pointer.
  2. The object is "big." The precise definition of "big" will depend on your computer, your platform, the ramifications of making a copy of your object, etc. You will have to evaluate on a case-by-case basis. Pass by const reference.
  3. Making a copy of the object would do something "bad". For example, if by copying a file pointer object it creates another file, or by copying a synchronization object it creates a deadlock. Pass by const reference.
  4. You need polymorphic behavior on the passed-in object. Pass by reference or by pointer. Passing by value will slice the object.

Note that with #2 above, even if the object is big you may still want to pass by value. Passing by value is simpler therefore easier to maintain, and unless you have reason to believe that passing by value is creating an actual problem in your code, passing by const reference might be premature micro optiminzation. For example, if your function takes a parameter that is 64 bytes (or even 64k bytes) but that function is only called once a day, there's really no reason to pass by const reference. It saves you virtually nothing. Profile your code in release mode first, then optimize.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜