C + + overloaded assignment operator

Source: Internet
Author: User

This is a C + + face test, the following in this blog to analyze the problem. First on the topic:

Title: The following is a declaration of type cmystring, please add an assignment operator function for that type. Class cmystring{public:cmystring (char *pdata=null);//constructor cmystring (const cmystring& str);//copy constructor ~cmystring () ;//destructor private:char* m_pdata;//data field, character pointer};

  

Get this topic, if you have read effective C + + This book, the code should be able to complete easily, when writing to pay attention to one of the following points:

The 1> assignment operator returns a reference to the instance itself (*this), because only one reference is returned to allow successive assignment operations.

2> declares the type of the passed-in parameter as a constant reference, which avoids the copy of the value when the instance is passed in, and avoids the call to duplicate the constructor. The assignment operator does not change the state of the incoming instance, so add the Const keyword before the incoming parameter.

3> to free up existing space before allocating new memory, or the program will have a memory leak. The assignment operator performs an assignment operation, which must have called the object's constructor or copy constructor before the assignment operator, because an object must have an initialization process when it is instantiated. the original heap memory space must be freed up, as the m_pdata may become longer and the original space may not be enough .

4> determines whether the passed-in parameter is the same instance as the current instance (*this), and returns directly if it is the same instance, preventing str1=str1 of such meaningless operations. If this is not judged, the result is serious, if the heap memory space of the instance is freed, the heap space of the parameter is disposed (although the parameter is const, but this time the release is released through the This object, not by the parameter object), there is no full backup of the object, Then this object will disappear forever from this world.

C + + overloaded assignment operator

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.