Assignment operator overloaded functions in C + + (operator=)

Source: Internet
Author: User

    mystr& operator = (const mystr& str)//assignment operator    {        cout << "operator =" << Endl;        if (this = &str)        {            if (name! = NULL)                delete name;            This->id = str.id;            int len = strlen (str.name);            Name = new Char[len + 1];            strcpy_s (name, strlen (str.name) + 1, str.name);        }        return *this;    }

. Parameters

In general, the argument to the overloaded function of the assignment operator is a reference to the const type of the class in which the function is located (as in Example 1 above), and the const is because:

① we do not want to make any changes to the "original" that is used to assign values in this function.

② Plus const, the function is acceptable for const and non-const arguments, and if not added, only non-const arguments can be accepted.

References are used because:

This avoids a single copy of the argument at the time of the function call, which improves efficiency.

Note :

The above rules are not mandatory, can be non-const, or can not be referenced, even the argument can not be the object of the function, as in Example 2.

. return value

In general, the return value is a reference to the assigned person, namely *this (example 1 above), because

① this avoids a single copy when the function returns and improves efficiency.

② More important, this allows for continuous assignment, which is similar to a=b=c. If you are not returning a reference but returning a value type, when executing a=b, call the assignment operator overload function, and when the function returns, because the value type is returned, make a copy of the "thing" behind the return and get an unnamed copy (some of which are referred to as "anonymous objects"). The copy is then returned, and the copy is an rvalue, so after executing the a=b, you get an rvalue, and then execute the =c error.

Note :

This is also not mandatory, we can declare the function return value as void, and then return nothing, just so it is not able to assign a value continuously.

. Call Timing

When assigning a value to a class object (note: You can assign a value to it using this class of objects (as in example 1 above), or you can assign a value to it with a value of another type (such as a built-in type), for this, see Example 2, later, this object invokes the assignment operator overload function for that class.

As in the above code

STR2 = str1;

, the assignment operator overload function for the MyStr class is called by str2, using STR1 to assign a value to str2.

It is important to note that

MyStr str2;

STR2 = str1;

And

MyStr STR3 = str2;

There is a difference in calling the function. As we saw in the results above.

The former mystr str2; the declaration of a str2, called the parameterless constructor, so str2 = str1; In the case of str2 already exists, STR1 is used to assign a value to STR2, and the copy assignment operator overload function is called; is to initialize the STR3 with STR2, which is called the copy constructor.

See more: https://www.cnblogs.com/zpcdbky/p/5027481.html

Assignment operator overloaded functions in C + + (operator=)

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.