References and pointer parameters, both of which allow the function to modify the real parameters, and allow the function to effectively pass large class objects. So how can we declare a function parameter as a reference or a pointer?
The reference must be initialized to point to an object, and once initialized, it cannot point to other objects. The pointer can point to a series of different objects, and can not point to anything.
Because the pointer may point to an object or does not have any objects, the function cannot safely reference a pointer before determining whether the pointer actually points to a valid object. For example:
Class X; void manip (x * px) {// determine whether it is not 0if (PX! = 0) // unreference pointer}
On the other hand, for reference parameters, the function does not need to point to an object. For example:
Class type {}; void operate (const type & P1, const type & p2); int main () {type obj1; // error: the real parameter of the referenced parameter cannot be 0 type obj2 = operate (obj1, 0 );}
If a parameter may point to different objects in a function, or this parameter may not point to any object, you must use the pointer parameter.
An important usage of referenced parameters is that it allows our colleagues who effectively implement the overloaded operators and ensures the directness of the usage.
Check the instance. It uses the matrix class type. We want to support the addition and assignment operators of two matrix objects so that their usage is "natural" like the built-in types ":
Matrix a,b,c;c = a + b;
Addition and value assignment operators of matrix objects are implemented using overload operators.
Provides a definition for the overload OPERATOR:
Matrix operator+(Matrix m1,Matrix x2){Matrix result;//do the computation in resultreturn result;}
Passing real parameters by value is very inefficient.
To improve efficiency, let's declare the parameter as a pointer. Operator + () new implementation code:
Matrix operator+(Matrix *m1,Matrix *x2){Matrix result;//do the computation in resultreturn result;}
There is a disadvantage: the intuition of addition operator usage is lost. Now, the pointer parameter requires us to pass the address as the real parameter and point to the matrix object. Now, our addition operations must be programmed as follows:
&a + &b;
However, this is ugly and the interface is unfriendly. In addition, it is difficult to add three objects to a composite expression:
// This cannot work. // The returned type of & A + & B is matrix object & A + & B + & C;
The program must be written as follows:
&(&a + &b) + &c;
However, the reference parameters provide the solutions we need. In this case, the function receives the left value of the real parameter instead of the value worth copying. The real parameter of the referenced parameter is the matrix object, which allows us to naturally use the addition operator like an object of built-in data type.
Operator + () implementation code:
Matrix operator+(Matrix &m1,Matrix &x2){Matrix result;//do the computation in resultreturn result;}
It supports addition of matrix objects in the following forms:
a + b + c;