C++,operator=

Source: Internet
Author: User
Tags shallow copy

Why is operator= worth noticing?

Syntactically speaking, the following program can be compiled and passed, and I use it in another note example.

classa1{ Public:    int operator=(intA//The argument is int, the syntax is passed but the logic does not pass.     {        return 8; }    int operator+(inta) {return 9; }    int operator= (A1 &a)//return int, meaningless or illogical,{cout<<"operator= (A1 &a)"<<Endl; return 0; }};
Http://www.cnblogs.com/mylinux/p/4094808.html

In fact, like the copy constructor, we need to consider more questions:

1. Shallow copy of the problem. [1]//Constructor, Operator= is the same as the constructor.

Precautions: [2]//effective_c++

2. Why opreator= to return to *this?
In order to support chain calls such as A=b=c
3. Why not return to const Goodobject&?
For operations that are compatible with compilers (A=B) =c
4. Why pass in const goodobject& RHS
The following operations are primarily supported:
Goodobject A;
Const Goodobject B;
A = b;
5. Why should there be a judgment like if (this! = &RHS)
The main thing is to avoid self-assignment a=a such a situation occurs.
6. Don't forget to rewrite the copy constructor

7. Example: [3]

Examples of reference users:

classsampleclass{Private:         intA; Doubleb; float*p; Public: SampleClass&operator= (Constsampleclass&s) {if( This= = &s)return* This;//a sentence to solve a self-assigned valueA =S.A.; b=s.b;                   Delete p; P=New float(*S.P); return* This; }};

This is written in the book://Although there is a TMP, but the recommended reference

sampleclass&operator= (Constsampleclass&s) {         if( This= = &s)return* This;//A =S.A.; b=s.b; float* tmp = p;//The old pointers are saved firstp =New float(*S.P);//then apply for new space, if the application fails, p still points to the original address spaceDelete tmp;//can walk here, indicating that the application space is successful, then you can delete the old content         return* This;}

Reference:

A brief analysis of 1.c++ depth copy
Http://www.jizhuomi.com/software/291.html

The 2.operator= function.

http://blog.csdn.net/howlet2/article/details/5090756

3. Reading notes _EFFECTIVE_C++_ clause 11: Handling Self-assignment in operator=
Http://www.cnblogs.com/jerry19880126/archive/2013/03/21/2972648.html

C++,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.