C + + operator overloaded assignment operator

Source: Internet
Author: User
Tags constructor shallow copy strlen

The assignment operator overload function of a custom class acts like a built-in assignment operator, note, however, that it is the same as a copy constructor and a destructor to note the problem of deep copy of a shallow copy, and without a deep copy of a shallow copy, if the default assignment operator overload function is not specified, Then the system will automatically provide an assignment operator overload function.

The definition of an assignment operator overloaded function is similar to that of other operator overloaded functions.

Here's an example of how to use it, with the following code:

//Program Author: Junning 


//site: www.cndev-lab.com


//All manuscripts are copyrighted, if you want to reprint, please be sure that the famous source and author


#include <iostream>


using namespace std;


class Internet


{


Public:


Internet (char *name,char *url)


{


internet::name = new Char[strlen (name) +1];


Internet::url = new Char[strlen (URL) +1];


if (name)


{


strcpy (internet::name,name);


}


if (URL)


{


strcpy (Internet::url,url);


}


}


Internet (Internet &temp)


{


internet::name=new Char[strlen (temp.name) +1];


internet::url=new Char[strlen (Temp.url) +1];


if (name)


{


strcpy (internet::name,temp.name);


}


if (URL)


{


strcpy (Internet::url,temp.url);


}


}


~internet ()


{


delete[] name;


delete[] URL;


}


internet& operator = (Internet &temp)//Assignment operator overloaded 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)


{


strcpy (this->name,temp.name);


}


if (this->url)


{


strcpy (This->url,temp.url);


}


return *this;


}


Public:


Char *name;


Char *url;


};


int main ()


{


Internet A ("China Software Development Laboratory", "www.cndev-lab.com");


Internet B = a;//b object does not yet exist, so call the copy constructor to construct the process.


cout<<b.name<<endl<<b.url<<endl;


Internet C ("AOL", "www.aol.com");


B = c;//b object already exists, so the system chooses the assignment operator to overload function processing.


cout<<b.name<<endl<<b.url<<endl;


System ("pause");


}

The internet& operator = (Internet &temp) in the example code above is the definition of an assignment operator overloaded function, and the internal need for the first delete pointer is where the problem of deep copying is involved, because the B object is already constructed, The scope of the name and URL pointers has been determined, so the heap area must be cleared before the new content can be copied, the area is too large and too small, so the heap size is redistributed later and the copy is done.

In cases where the class object does not yet exist, the assignment process is constructed by copying the constructor (Internet B = A in code), but when the object already exists, the assignment is handled by overloading the function with the assignment operator (b = C in the example; this is the case).

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.