The return value of a function seems simple, but not so. For example:
Int func (int A); this function returns an int type. If you call int result = func (3); what happens?
First, func copies the returned value to an anonymous temporary variable. Here we assume that the temporary variable is anony (in fact, there is no name, so it is easy to elaborate here). Then, copy the anony value to the result. We can see that the anony value is copied twice. Instead of one copy.
For the function that returns the reference:
Int & func (Int & A); assume that the function is passed in an int reference, then modify it in func, and then return its reference. If int reslut = func (B) is called ); the following situations may occur:
The returned result is a reference of B, which is equivalent to directly copying the value of B to the result. Here there is only one copy (with the Temporary Variable missing, of course, a temporary variable is also created, but this temporary variable is a reference of B ).
Note that, according to many people's understanding, a reference is returned here, so the result is the reference of B. In fact, this is not the case, the returned reference only reduces the replication of temporary variable values. If you really want result to be able to reference B, you can do this: Int & Result = func (B );
Note: returning a reference to a common variable does not show any difference in efficiency, but the efficiency difference is obvious when returning a large class or struct.
If so, declare the int func (int A) function. Note that the returned result is not a reference. Then what happens to Int & Result = func (?
If so, the compiler will report an error: a temporary variable cannot be used to initialize a very large number of referenced variables.
To eliminate this error, you can write const Int & Result = func (a); in this way, although the returned result is not a reference, it is assigned a reference variable, therefore, there is only one replication process in the return process. However, such a result cannot be modified.
There is another seemingly strange but reasonable situation:
Int & func (Int & A); similarly, if this function is passed in an int reference, modify it in func and return its reference. Then call func (B) = 3 in this way; the consequence is that the value of passed B is changed to 3. The reason is that func returns a reference to B and then assigns the reference to 3. Therefore, the value of B is changed to 3.
If you want to disable sending in this case, you can declare the function: const Int & func (Int & A); in this way, a const reference is returned, it cannot use this reference to modify the value it points. Therefore, if there is a call like func (B) = 3, the compilation will fail.
For example:
String Foo (); void bar (string & S) // The following expression will be invalid: bar (FOO (); Bar ("Hello World ");
Both Foo () and "Hello World" strings generate a temporary object. In C ++,These temporary objects are of the const type.. Therefore, the above expression tries to convert a const type object to a non-const type, which is invalid. The referenced parameter should be defined as const if it can be defined as Const.
From: http://blog.csdn.net/piratejk/article/details/6162554