Overload assignment operator

Source: Internet
Author: User

[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 ).

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.