I. Definitions of references and pointers
Reference: It takes an alias to another variable and does not allocate space again (which can lead to optimization of the program)
Pointer: It is an entity that needs to allocate space
references must be initialized at the time of definition, and space cannot be changed .
The pointer does not have to be initialized at the time it is defined, and the space it points to is variable . (Note: The referenced value cannot be null)----------> can bring program security
A reference to access a variable is directly accessible, while a pointer to access a variable is an indirect access
Second, C + + initialization function 6 default functions
1. Constructors
2. Destructors
3. Copy constructors (when function arguments are objects)-------> shallow copy deep copy
Text T1;
Text T2 (T1)
Text t2 = t1;
4. Assignment function-----------> Shallow Assignment depth value
Text T1;
Text T2;
t2 = T1;
5.Text A;
Text *b = &a;
text* operator& () {return this};
6.const Text A (constant object);
Const Text *B = &a;
Const text* operator () {return this};
Third, deep copy, shallow copy
Execution procedure: Call the constructor once, customize the copy constructor once, and two times the destructor. A pointer member of two objects refers to a different memory.
A shallow copy is just a copy of the pointer, and the two pointers point to the same memory space
A deep copy not only copies the pointer, but also copies the contents of the pointer, which is a pointer to two different addresses.
Student::student () { name = new char (); cout << "Student" << Endl;} Student::~student () { cout << "~student" << (int) name << Endl; Delete name; name = NULL;} Student::student (const Student &s) { name = new char (a); memcpy (name, S.name, strlen (s.name)); cout << "Copy Student" << Endl;}
The difference between C + + references and pointers