Overload assignment operator & amp; object, overload assignment

Source: Internet
Author: User

Reload assignment operator & object, reload assignment

Class CMessage {private: char * m_pMessage; public: void showIt () const {cout <m_pMessage <endl ;} // const char * text = "Default message") {cout <"Constructor difinition" <endl; size_t length {strlen (text) + 1 }; m_pMessage = new char [length + 1]; strcpy_s (m_pMessage, length + 1, text);} // copy the constructor CMessage (const CMessage & aMess) {size_t len {strlen (aMess. m_pMessage) + 1}; this-> m_pMessage = New char [len]; strcpy_s (m_pMessage, len, aMess. m_pMessage);} // overload value assignment operator CMessage & operator = (const CMessage & aMess) {if (this! = & AMess) {delete [] m_pMessage; size_t length {strlen (aMess. m_pMessage) + 1}; m_pMessage = new char [length]; strcpy_s (this-> m_pMessage, length, aMess. m_pMessage);} return * this;} // destructor ~ CMessage () {cout <"Destructor called" <endl; delete [] m_pMessage ;}}; int main () {CMessage motto1 {"Amiss is as good as a mile"}; CMessage motto2; motto2 = motto1; motto2.showIt (); motto1.showIt (); return 0 ;}

Copy constructor:

When a class dynamically allocates space for data members and transmits the objects of the class to the function by value, the replication constructor must be implemented.

For example:

Motto2 {motto1 };


CMessage & displayMessage (CMessage localMsg)
Cout <"the message is: ----------------" <endl;
LocalMsg. showIt ();
Return * this;

An exception occurs. If the replication constructor is not implemented, the pointer of the two objects points to the same address area.

Overload assignment operator:

Initialize an object of the same type as an existing object, or pass the object to the function by passing the value, call the default copy constructor.

When the left and right sides of the value assignment statement are objects of the same type, the default value assignment operator is called.

For example:

Motto2 = motto1;

If we do not implement the value assignment operator function, the compiler will use the default value assignment operator function. When dynamically allocating memory to data members and assigning values to objects, the program will be abnormal.

Because both objects have a pointer to the same memory address.

Analysis of assignment operator functions:

CMessage & operator = (const CMessage & aMess)

{If (this! = & AMess)


Delete [] m_pMessage;

Size_t length {strlen (aMess. m_pMessage) + 1 };

M_pMessage = new char [length];

Strcpy_s (this-> m_pMessage, length, aMess. m_pMessage );


Return * this;


Delete [] m_pMessage; delete the memory allocated to the first object and re-allocate enough memory to hold the string of the second object.

1. What happens when motto1 = motto1?

The value assignment operator function releases the memory pointed to by the motto1 object member.

Therefore, if (this! = & AMess) This statement is necessary.

2. Why?

Motto1 = motto2 = motto3;

The prototype of this statement is:

Motto1.operator = (motto2.operator = (motto3 ));

We can see that the result of motto2.operator = (motto3) must be an object to be used as a parameter of motto1.operator =.

3. Why is the reference returned?

(Motto1 = motto2) = motto3;

The prototype of this statement is:

(Motto1.operator = (motto2). operator = (motto3 );

We can see that (motto1.operator = (motto2) is a temporary object returned, Which is rvalue. The Compiler does not allow calling function members using rvalue.

The return value references lvalue.

Related Article

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.