this pointer:
There is an implicit pointer variable this in the formal parameter list of each member function of the class. The type of the pointer variable is the type of the class to which the member function belongs
When the member function of the class is called in the program, the this pointer variable is automatically initialized to the address of the object issuing the function call
Use of this:
1. Distinguish between members and non-members
2. A method of a class needs to return a reference to the current object
QS& set(int n, string name) {
this->n = n;
this->name = name;
return *this;
}
A simple analysis of the second method:
#include <iostream>
#include<string>
using namespace std;
class QS {
private://If omittedprivateMeansprivateAttributes
int n;
string name;
public:
QS() {
this->n = 1;
name = "wang";
cout << "Constructor" << endl;
}
QS& set(int n, string name) {
this->n = n;
this->name = name;
return *this;
}
~QS(){
cout << "Destructor" << endl;
}
QS yyj(QS j)
{
j.n = this->n + 1;
j.name = j.name + "+yyj";
return j;
}
void show() {
//printf("number=%d name=%s\n", this->n, this->name.c_str()); Use c_str() to change the string type name into a character array
cout << "number= " << this->n << " name= " << this->name << endl;
}
};
int main()
{
QS s;
s.show();
QS&ppt=s.set(100,"xiaobai");//There is no class that defines the name of ppt, but ppt points to s, which realizes data sharing and co-editing
ppt.show();
s.show();
QS s1;
s1.show();
s1 = s.yyj(s1);//Here is that the temporary variable is not constructed but has a destructor
s1.show();
return 0;
}

Here, this->n=n assigns the integer variable n defined by this function to the private member n of this object
this->name=name is the name of the string type defined by this function is assigned to the private member name of this object
The results are as follows:
by
It can be seen that the defined QS& set function does not construct a new class, but directly points ppt to s, which realizes data sharing and co-editing
The first two destructors are temporary objects generated when the yyj function is called, and these objects will not call the constructor and can be assigned to realize the call of the copy constructor
Hibernate Usage Important Notes Hibernate API introduction: Configuration class SessionFactory interface: It is best to create only one factory (unless it is a multi-data source) Transaction interface...
When starting producers and consumers, they report an error: 1) View the Kafka configuration file, cat config/server.properties The connected zookeeper is localhost, so you need to start the productio...
Article directory Connect redis Basic grammar Key String Connect redis Basic grammar Key # command effect 1 del key Delete key 2 dump key Serialize the key and return the serialized value 3 exists key...
List is a doubly linked list, header file #include<list> 1. Basic operation insert:Headend, tailend, middle (through iterators) Find:There is no member function, only the iterator can be returne...
1, must bind the host name and IP, otherwise the remote will not connect vi /etc/hosts 2, set ssh free login (1) ssh-keygen, always enter (2)ssh-copy-id localhost 3, install Java (1) Install jdk1.8: (...
1. Use the unequal operator (<>, !=) In the following case, even if the column dept_id has an index, the query still performs a full table scan. select * from dept where staff_num <> 1000;...
Thanks to Dowager A's FusionCharts Free Chinese Development Guide, I have taken a lot of detours. But there is still a difficulty, how to use smart strings and splicing dynamic data into dataXml in an...
paperclip Is a plugin for rails processing attachments, compared to the pastattachment_fuWaiting for better efficiency and use. paperclipUploaded image attachmen...
Need to delete large amounts of data from time to time because of work needs Due to the ability problem, I am afraid to schedule the deletion. And due to performance issues, only 4G undo tablespace on...
Presumably everyone in the project development, debugging class file modification, the container automatically reloading the long process has long been tired, I am free today, so I want to try javaReb...