The assignment operator for C + + replication control

Source: Internet
Author: User



Overloaded operators

The overloaded operator is a function whose name is operator followed by its defined operator symbol. such as operator =, operator +, operator * and so on.

The operator function, like the normal function, is also the return value + function name + (formal parameter list), and the formal parameter list must have the same parameter as the operator operand (if the operator is a member, the implicit this parameter is included).


Assignment operator

The assignment is a two-tuple operation, so the operator function has two parameters: the first parameter corresponds to the left operand, and the second parameter corresponds to the right operand.

Most operators can be defined as member functions or non-member functions. When the operator is a member function, its first operand is implicitly bound to the this pointer. Some operators, including assignment operators, must be members of the class that defines them. Because the assignment must be a member of the class, this is bound to a pointer to the left operand. Therefore, the assignment operator accepts a single parameter, and the parameter is an object of the same class type. The right operand is typically passed as a const reference.


Synthetic assignment operators

The synthetic assignment operator is similar to the action of the synthetic copy constructor, which performs a member-by-value assignment: Each member of the right operand object is assigned to the corresponding member of the left operand object. In addition to the divisor group, each member is assigned in the usual way that it belongs. For arrays, each array element is assigned a value.

Class Test

{

Private

int i;

Double D;

}

The synthetic assignment operator for this class might look like this:

test& test::operator= (const test& right)

{

I=RIGHT.I;

D=RIGHT.D;

return *this;

}

The synthetic assignment operator returns a reference to the left operand object.


Note: Replication and assignment should be treated as a unit, and if one is needed, we almost certainly need another.


What is the difference between a copy constructor and an assignment operator?

Both the copy constructor and the assignment operator use an existing object A to create another object B, except that the copy constructor creates a completely new object, that is, B does not exist before the copy constructor is called, and the assignment operator handles two existing objects, that is, the pre-assignment B is present. For example:

Test A (B); Copy constructor

Test c= D; Copy constructor

Test E;

E= A; Assignment operators


<span style= "FONT-SIZE:18PX;" > Write a function to verify the:</span>
<span style= "FONT-SIZE:18PX;" >  </span><pre name= "code" class= "CPP" ><span style= "font-size:18px;" > #include <iostream>using namespace std;class exam{public    :   Exam () {}   Exam (const exam& Other   {          cout<< "This was a Exam (const exam& Other)" <<endl;   }   exam& operator= (const exam& Other)   {      cout<< ' This is a operator= ' <<endl;      return *this;}   ; int main () {  Exam A;  Exam B (A);  Copy constructor  Exam c= A;  Copy constructor  Exam D;  Overloaded assignment operator  d= A;  return 0;} </span>


The results of the operation are as follows:



Ok!

The assignment operator for C + + replication control

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.