Assignment operator overloading

Source: Internet
Author: User
Tags shallow copy

For assignments of non-intrinsic class objects, there is a default assignment operator overload function.

As in the following code, C2=C1 calls the default assignment operator overload function of Class A, which implements the C2.real=c1.real; C2.image=c1.image;

classa{Private:      intReal; intimage;  Public: A (intRinti) {real=R; Image=i; }};intMain () {A C1 (1,2); A C2 (0,0); C2=C1;}
  • What is the time when an assignment operator overload function is explicitly defined?
    Explicit definition is required when there is a dynamically allocated storage space in a class member variable. Otherwise, the same problem as the shallow copy, C1 to the C2 assignment, C1 and C2 pointers to the same memory space, destruction, the same space is destroyed two times, there is a run error.

  • What is the difference between an assignment operator overload function and a copy constructor? (example code below)
    1) The assignment operator overload function needs to release the original contents of the object's own space first, and then, according to the C1 of the dynamic request of the assignment object, the C2 dynamically applies for the same size space, and then assigns the value;
    Copy constructor only need to directly according to C1 in the dynamic request of the memory size, to C2 dynamically request the same size of space, to assign value, do not need to C2 first release, because C2 is not initialized, copy constructor is in the C2 initialization of the call, A c2=c1;
    2) When the call time is different, the copy constructor is called at initialization, and the assignment operation is only called when the assignment is assigned.

    Copy constructor and assignment operator overloading:
    classa{Private:    Char*name; Char*NAMEP; Public: A (a&A1) {        if(a1.name) {name=New Char[Strlen (A1.name) +1];        strcpy (Name,a1.name); }          if(A1.NAMEP) {NAMEP=New Char[Strlen (A1.NAMEP) +1];        strcpy (NAMEP,A1.NAMEP); }} A&operator= (A &A1) {         if(name)Delete[]name; if(NAMEP)Delete[]NAMEP; if(a1.name) {name=New Char[Strlen (A1.name) +1];         strcpy (Name,a1.name); }         Elsename=NULL; if(A1.NAMEP) {NAMEP=New Char[Strlen (A1.NAMEP) +1];         strcpy (NAMEP,A1.NAMEP); }         ElseNAMEP=NULL; return* This;                                    }}; 

  • Parameter discussion of overloaded function of assignment operator;
    The parameter of an assignment operator is usually a reference to this class of objects, because the object may occupy a large amount of memory, using this class of object references, eliminating the stack space required by the temporary object. The copy constructor is not called, and the copy time is omitted.
    Of course, in order to avoid changing the value of the parameter, a const-qualified reference is added. The formal parameter is therefore a constant reference to this class of objects. const a& A1;

  • Assignment operator overload function return value discussion:
    The return value can be: Void type return, this class object returns, this kind of object's reference returns;
    1) return void type. The assignment works fine, but because there is no return value, the assignment expression cannot be assigned consecutively.
    2) Returns the object of this class. A temporary object is created on return, the copy constructor is called to initialize the temporary object, and after the function ends, the temporary object is destroyed;
    3) Returns a reference to the object of this class. There is no need to initialize the memory temporary object, the return value is the object itself, that is, *this.

Assignment operator overloading

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.