// Overload operator +
Vector3 operator + (const Vector3 & ){
Return Vector3 (x + a. x, y + a. y, z + a. z );
}
// Compare two numbers. If they are the same, 0 is returned. If v1 is large, 1 is returned. If v2 is large,-1 is returned.
Int compare (const int & v1, const int & v2)
{
If (v1 <v2) return-1;
If (v2 <v1) return 1;
Return 0;
}
Because C # and Java are used in the past, we cannot understand the parameter definitions of these two functions. Why should we add const and &. In fact, sometimes the ampersands such as "&", "*", and "T" are too big. If I use the second function in java, I will write it like this:
// Compare two numbers. If they are the same, 0 is returned. If v1 is large, 1 is returned. If v2 is large,-1 is returned.
Int compare (int v1, int v2) www.2cto.com
{
If (v1 <v2) return-1;
If (v2 <v1) return 1;
Return 0;
}
Regardless of the above, let's look at an example of countless exchanges:
[Cpp] view plaincopy
// Define a parameter as a reference type
Void swapOne (int & a, int & B)
{
Int tmp = B;
B =;
A = tmp;
}
// Use the copy real parameter to change the local copy, but the real parameter of the passed function is not modified.
Void swapTwo (int a, int B)
{
Int tmp = B;
B =;
A = tmp;
}
Int main (int argc, char * argv [])
{
Int a = 1, B = 2;
Printf ("before swap, a: % d, B: % d \ n", a, B );
SwapOne (a, B );
Printf ("after swap, a: % d, B: % d \ n", a, B );
Printf ("before swap, a: % d, B: % d \ n", a, B );
SwapTwo (a, B );
Printf ("after swap, a: % d, B: % d \ n", a, B );
Return 0;
}
The result is as follows:
The result shows that the swapOne function successfully exchanges the and B variables with reference parameters. The swapTwo function is not successfully switched.
SwapTwo uses common non-reference parameters, which are initialized by copying the corresponding real parameters. The real parameters passed in the function are not changed.
In swapOne, the referenced parameters are directly associated with the bound objects, rather than copies of these objects. Therefore, the exchange is successful.
[Cpp] view plaincopy
// Compare two numbers. If they are the same, 0 is returned. If v1 is large, 1 is returned. If v2 is large,-1 is returned.
Int compare (const int & v1, const int & v2)
{
If (v1 <v2) return-1;
If (v2 <v1) return 1;
Return 0;
}
What is the role of const in the above example? The original use of const can avoid reference replication and improve performance. Of course swapOne cannot be used because it needs to change the value, while the compare function only compares the size of two numbers without switching, therefore, adding const can improve the performance. Because copying objects takes time and storage space.
Note: I recently accidentally saw Article 20 "pass-by-reference-to-const to replace pass-by-value" in Objective C ++ ". This issue was also discussed.
[Cpp] view plaincopy
Class Person {
Public:
Person ();
Virtual ~ Person ();
...
Private:
Std: string name;
Std: string address;
};
Class Student: public Person {
Public:
Student ();
~ Student ();
...
Private:
Std: string schoolName;
Std: string schoolAddress;
}
Suppose there is such a function
Bool validateStudent (Student s );
If the by Value method is used
Student plato;
Bool platoIsOk = validateStudent (plato );
Because the Student class inherits the Person class, each class has two strings. The book says that the total cost of passing a Student object by value is "Six constructor and six constructor "!
If it is changed to pass by reference-to-const:
Bool validateStudent (const Student & s); no constructor or destructor is called because no new object is created.