refer to
project | |
---|---|
Mastering C++ (Ninth Edition) | Tony Gaddis, Judy Walters, Godfrey Muganda (Author) / Huang Gang et al. (Translation) |
search engine | Bing |
describe
project | describe |
---|---|
translater | gcc version 8.1.0 (x86_64-win32-seh-rev0, Built by MinGW-W64 project) |
operating system | Windows 10 Pro (64-bit) |
implicit parameter this pointer
By default, the compiler provides each member function of a class with an implicit parameter that points to the object on which the called member function resides. This implicit parameter is called this .
The above is quoted from Mastering C++, Ninth Edition
The implicit parameter this is a pointer to the object where the function is located. You can use the implicit parameter this directly in member functions. In fact, when you directly access the members of the class in the member function, you have implicitly used the this pointer.
give a chestnut
#include <iostream> #include <string> using namespace std; class Notebook { public: string content = "Hello World"; void echo(){ // Output the address of the class to which it belongs (displayed in decimal) cout << (long long)this << endl; // Output the content member attribute of the class to which it belongs cout << this -> content << endl; }; }; int main(){ // instantiate Notebook notebook; notebook.echo(); system("pause"); }
Execution effect
6422000 Hello World Please press any key to continue. . .
Note:
The this pointer can only point to the object instantiated by the class to which it belongs, and the operation of assigning the actual parameter to the formal parameter will be automatically completed during the process of creating the instance object of the class.
conflict
When the formal member of the member function has the same name as the member property, a conflict will occur inside the member function, and the member property will be hidden. For this, please refer to the following example:
#include <iostream> #include <string> using namespace std; class Notebook { public: string content = "TwoMoons"; void read(string content){ // output content cout << content << endl; }; }; int main(){ Notebook notebook; notebook.read("RedHEart"); system("pause"); }
Results of the
RedHEart Please press any key to continue. . .
In the member function, the data stored in content is output to the console, and the result is to output the content in the formal parameter content, while the member attribute content is ignored.
this pointer
Through the this pointer, we can break through the blockade of the formal parameter with the same name in the conflicting member function and access the member attribute with the same name. For this, please refer to the following example:
#include <iostream> #include <string> using namespace std; class Notebook { public: string content = "TwoMoons"; void read(string content){ // output content cout << content << endl; // output member attribute content cout << this -> content << endl; }; }; int main(){ Notebook notebook; notebook.read("RedHEart"); system("pause"); }
Results of the
RedHEart TwoMoons Please press any key to continue. . .
constant parameter
pass by value vs pass by reference
When passing parameters to a function, we have two options, pass by value or pass by reference.
pass by value vs pass by reference
- Pass by value passes a copied copy of the actual parameter to the associated formal parameter.
- Pass by reference passes the address of the actual parameter to the formal parameter.
Compare
Passing by value will have a greater impact on the performance of the program when the actual parameters are complex (the amount of data is large, and the copy operation will consume a certain amount of performance). The address is passed by reference, and we can directly access the memory space pointed to by the actual parameter through the address stored in the formal parameter. Since the formal parameters and actual parameters point to the same memory space, the possibility of unintentionally modifying the data in the memory space is increased.
solve
In order to prevent the formal parameter from modifying the data in the memory space pointed to by the actual parameter, we can set the formal parameter as a constant. C++ will throw an error if you try to modify a constant parameter.
constant parameter
give a chestnut
#include <iostream> #include <string> using namespace std; void fraud(const int &num1, int &num2){ // num1 cannot be set as a constant // modified by the function. Therefore execute // num1 += 100; // will cause C++ to throw an error. num2 += 1; cout << (num1 + num2) << endl; }; int main(){ int num1 = 1; int num2 = 1; fraud(num1, num2); system("pause"); }
Execution effect
3 Please press any key to continue. . .
Promise (constant formal parameter passing problem)
A function that promises not to modify X cannot pass X to another function unless the second function also promises not to modify X.
The above is quoted from Mastering C++, Ninth Edition
For this, please refer to the following example:
#include <string> #include <iostream> using namespace std; void underling(int &num){ // Although the underling function does not promise to modify the formal parameters, // But the function does not modify the formal parameters because of this. But even so, // C++ will also throw an error. } // fraud functions are committed via the keyword const // The formal parameter num is not modified void fraud(const int &num){ // fraud passes a formal parameter that promises not to be modified to // There is no function that promises not to modify the formal parameter num. // This will throw an error. Even if underling doesn't // Modify the formal parameter num . underling(num); } int main(){ int num = 666; fraud(num); cout << num << endl; system("pause"); }
In order for the function underling to receive actual parameters normally, we must promise not to modify the formal parameters. For this, please refer to the following example:
#include <string> #include <iostream> using namespace std; // underling promises not to modify parameters, so // underling successfully received the fraud function promise // Parameters that are not modified. The program runs normally from here. void underling(const int &num){ } // fraud functions are committed via the keyword const // The formal parameter num is not modified void fraud(const int &num){ underling(num); } int main(){ int num = 666; fraud(num); cout << num << endl; system("pause"); }
Execution effect
666 Please press any key to continue. . .
const member function
Regular member functions are member functions that cannot modify the object to which they belong. To declare a constant member function in a class, just add the keyword const after the function header of the function.
give a chestnut
#include <iostream> #include <string> using namespace std; class Notebook { public: string content = "Hello World"; void visitor() const { // Since the function has been declared as a constant member function, // Therefore, using the following two statements will cause C++ to throw an error. // this.content = "TwoMoons"; // content = "TwoMoons"; }; }; int main(){ Notebook notebook; cout << notebook.content << endl; notebook.visitor(); cout << notebook.content << endl; system("pause"); }
Execution effect
In a constant member function, an exception will be thrown whether you modify the owning object through an implicit parameter or not.
Hello World Hello World Please press any key to continue. . .