开发者

Is it always better to use pass by reference in C++? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicates:

“const T &arg” vs. “T arg”

How to pass objects to functions in C++?

I used the following code to ascertain that C++ on passing objects as const reference the compiler does not make a copy of the 开发者_JAVA百科object and send the copy. The output confirmed that passing object as const reference does not involve making a copy of the object.

Is there any situation in which passing objects as reference is worse than passing objects as value ( from an efficiency perspective ) ?

class ABC { 

public:

  int abc_1;
  int abc_2;

};

void swapABC ( ABC & _abc_ ) {

  _abc_.abc_2 = ( _abc_.abc_1 + _abc_.abc_2 ) - ( _abc_.abc_1 = _abc_.abc_2 ) ;

  printf ( "%d\n", ( void * ) &_abc_ ) ;

}

int main ( ) {

  ABC x = { 1,2 };
  swapABC ( x );
  printf ( "%d\n", ( void * ) &x ) ;

  return 0;

}


Sure.

Suppose all you want to do is pass in the value of an int. If it is passed by value then then that value gets pushed on the stack (or put in a register, however your calling convention does it). On the inside the value is retrieved straight into its destination register ready for use.

If you were instead to pass it by const reference, what gets passed in is the address of where the program can go to find the value when it wants it. That means in order to get the actual value, your program will have to do an entire extra memory fetch. It doesn't take much extra time, but it is more work that wasn't really needed.

Also, with a value pass you are utterly breaking any dependency between caller and callee at the point of the call. That means that when compiling the caller's code, the compiler may safely ignore anything the caller might do to or need from that variable outside of that one point where the call is made. With a reference parameter (even a const one), the compiler now has to worry about what might happen if for example the caller saves off the address of that variable for later use by other routines in other source files. The result is that is is much easier to optimize the caller's code when value passing is used, and the compiler is liable to be able to do a better job of it.


Look at this link which describes exactly what you want to know. Basically, if you have to do the copy, pass by value.


If you pass by non-const reference, you're saying that your function will modify the argument passed.

If you pass by const reference, then don't forget that the cost of passing will be a copy of a pointer (in almost all implementations), so it's not worth if you're passing an basic type.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜