[Cpp] view plaincopyprint?
# Include <iostream>
# Include <cstring>
Using namespace std;
Class Internet
{
Public:
Internet (char * name, char * url)
{
This-> name = new char [strlen (name) + 1];
This-> url = new char [strlen (url) + 1];
If (name! = NULL)
{
Strcpy (this-> name, name );
}
If (url! = NULL)
{
Strcpy (this-> url, url );
}
}
Internet (Internet & temp)
{
This-> name = new char [strlen (temp. name) + 1];
This-> url = new char [strlen (temp. url) + 1];
If (this-> name)
{
Strcpy (this-> name, temp. name );
}
If (this-> url)
{
Strcpy (this-> url, temp. url );
}
}
~ Internet ()
{
Delete [] this-> name;
Delete [] this-> url;
}
Internet & operator = (Internet & temp) // value assignment operator overload Function
{
Delete [] this-> name;
Delete [] this-> url;
This-> name = new char [strlen (temp. name) + 1];
This-> url = new char [strlen (temp. url) + 1];
If (this-> name! = NULL)
{
Strcpy (this-> name, temp. name );
}
If (this-> url! = NULL)
{
Strcpy (this-> url, temp. url );
}
Return * this;
}
Public:
Char * name;
Char * url;
};
Int main ()
{
Internet a ("Wang shihui's column", "blog.csdn.net/shihui512 ");
Internet B = a; // The object B does not exist. Therefore, you can call the copy constructor to construct the constructor.
Cout <B. name <endl <B. url <endl;
Internet c ("Qingshui Riverside Internet", "bbs.qshpan.com ");
B = c; // The B object already exists. Therefore, the system selects the value assignment operator to overload function processing.
Cout <B. name <endl <B. url <endl;
}
/*****************************
Running result:
Wang shihui's column
Blog.csdn.net/shihui512
Qingshui Riverside Internet
Bbs.qshpan.com
******************************/
# Include <iostream>
# Include <cstring>
Using namespace std;
Class Internet
{
Public:
Internet (char * name, char * url)
{
This-> name = new char [strlen (name) + 1];
This-> url = new char [strlen (url) + 1];
If (name! = NULL)
{
Strcpy (this-> name, name );
}
If (url! = NULL)
{
Strcpy (this-> url, url );
}
}
Internet (Internet & temp)
{
This-> name = new char [strlen (temp. name) + 1];
This-> url = new char [strlen (temp. url) + 1];
If (this-> name)
{
Strcpy (this-> name, temp. name );
}
If (this-> url)
{
Strcpy (this-> url, temp. url );
}
}
~ Internet ()
{
Delete [] this-> name;
Delete [] this-> url;
}
Internet & operator = (Internet & temp) // value assignment operator overload Function
{
Delete [] this-> name;
Delete [] this-> url;
This-> name = new char [strlen (temp. name) + 1];
This-> url = new char [strlen (temp. url) + 1];
If (this-> name! = NULL)
{
Strcpy (this-> name, temp. name );
}
If (this-> url! = NULL)
{
Strcpy (this-> url, temp. url );
}
Return * this;
}
Public:
Char * name;
Char * url;
};
Int main ()
{
Internet a ("Wang shihui's column", "blog.csdn.net/shihui512 ");
Internet B = a; // The object B does not exist. Therefore, you can call the copy constructor to construct the constructor.
Cout <B. name <endl <B. url <endl;
Internet c ("Qingshui Riverside Internet", "bbs.qshpan.com ");
B = c; // The B object already exists. Therefore, the system selects the value assignment operator to overload function processing.
Cout <B. name <endl <B. url <endl;
}
/*****************************
Running result:
Wang shihui's column
Blog.csdn.net/shihui512
Qingshui Riverside Internet
Bbs.qshpan.com
******************************/
The correct and safe version of the MyClass assignment operator wocould be this:
MyClass & MyClass: operator = (const MyClass & rhs ){
// Check for self-assignment!
If (this = & rhs) // Same object?
Return * this; // Yes, so skip assignment, and just return * this.
... // Deallocate, allocate new space, copy values...
Return * this;
} Or, you can simplify this a bit by doing:
MyClass & MyClass: operator = (const MyClass & rhs ){
// Only do assignment if RHS is a different object from this.
If (this! = & Rhs ){
... // Deallocate, allocate new space, copy values...
}
Return * this;
} Remember that in the comparison, this is a pointer to the object being called, and & rhs is a pointer to the object being passed in as the argument. so, you can see that we avoid the dangers of self-assignment with this check.
In summary, the guidelines for the assignment operator are:
Take a const-reference for the argument (the right-hand side of the assignment ).
Return a reference to the left-hand side, to support safe and reasonable operator chaining. (Do this by returning * this .)
Check for self-assignment, by comparing the pointers (this to & rhs ).