Passing object names directly
When a function argument is made with an object name, a new object is created when the function is called, which is a copy of the formal parameter object.
================ below gives an example program for directly passing an object name 1.1==================
Copy Code code as follows:
#include <iostream>
using namespace Std;
Class time
{
Public
Time (int,int,int);//constructor
void Print ();//Output information function
void Reset (Time t);//Reset function
Private
int year;
int month;
int day;
};
Time::time (int y,int m,int D)
{
Year=y;
Month=m;
Day=d;
}
void time::P rint ()
{
cout<<year<< "/" <<month<< "/" <<day<<endl;
}
void Time::reset (Time t)
{
t.year=0;
t.month=0;
t.day=0;
}
int main ()
{
Time T1 (12,12,12);//define an object and initialize it
T1. print ();//output T1 data member
T1.reset (t1)//Reset data member in T1
T1. print ();//output data member in T1
return 0;
}
Run Result:
From the results of the operation, the Reset function does not play a role.
The argument passes the value to the formal parameter, which takes up different storage space. The value of the argument is not evaluated regardless of whether the formal parameter is modified. This form of combination of real and actual, to produce a copy of the argument, when the size of the object is relatively large, the time and space costs can be very large.
Therefore, although this method is feasible, it does not advocate this usage ~
A parameter is a reference to an object
If the parameter is the reference name of the object, when an argument is an object name, it is not a separate storage space for the formal parameter (often called a copy of the argument), but rather the address of the argument variable to the formal parameter (the reference name), so that the reference name also points to the argument variable.
For program 1.1, we simply declare the parameter of the Reset function as a reference to the object.
Copy Code code as follows:
#include <iostream>
using namespace std;
Class time
{
public:
time (Int,int,int);//Constructor
void Print ();//Output Information letter Number
void Reset (time &t);//reset function ============ This line of code to modify ======================
private: Br> int year;
int Month;
int day;
};
Time::time (int y,int m,int d)
{
year=y;
month=m;
day=d;
}
Void Time::P rint ()
{
cout<<year<< "/" <<month<< "/" <<day< <endl;
}
Void Time::reset (Time &t)//============== modifies this line of code =================
{
t.year=0;
t.month=0;
t.day=0;
}
int main ()
{
time T1 (12,12,12);
t1. Print ();
t1.reset (t1);
t1. Print ();
return 0;
}
Run Result:
A formal parameter is a constant reference to an object
If we declare a function as an object reference, we can also declare it as a const (often referenced)
Copy Code code as follows:
void Reset (const time &t);
You can use only the data members and member functions in object T in a function, not the member functions in them, that is, the value of the data member in its corresponding argument.
Copy Code code as follows:
#include <iostream>
using namespace std;
Class time
{
public:
time (Int,int,int);//Constructor
void Print ();//Output Information letter Number
void Reset (const time &t);//Reset function
private:
int year;
int Month;
int day;
};
Time::time (int y,int m,int d)
{
year=y;
month=m;
day=d;
}
Void Time::P rint ()
{
cout<<year<< "/" <<month<< "/" <<day< <endl;
}
void Time::reset (const time &t)
{ //since it declares that T is a constant reference to an object, You cannot modify the value of its data member, so the following three lines of code are wrong ...
t.year=0;
t.month=0;
t.day=0;
}
int main ()
{
time T1 (12,12,12);
t1. Print ();
t1.reset (t1);
t1. Print ();
return 0;
}
The program will make an error, because in the function reset, attempt to modify the value of the data member in the object T