C ++-based overload assignment operator

Source: Internet
Author: User

To solve the problem above, we should write a special value assignment operator function to deal with such problems. When you need to assign values to two objects in the same class, you can overload the operator function. This method can solve the problem of class assignment and pointer release.

In the following program, the value assignment function in the class assigns a different pointer from the heap using the new operator. the pointer obtains the corresponding value of the value assignment object and then copies the value to the object that receives the value assignment.

The format of the overload assignment operator in the class is as follows:

Void operator = (const Date &) will be improved later. Currently, the return type of the overloaded operator function is void. It is a member function of the class. It is red in this program and is a member function of the Date class. Its function name is always operator =, and its parameters are always referenced by objects of the same class. The parameter indicates the source object, that is, the provider of the value assignment data. The operator of the overload function is used as a member function of the target object.

# Include "iostream. h"

# Include "string. h"

Class Date

{

Int mo, da, yr;

Char * month;

Public:

Date (int m = 0, int d = 0, int y = 0 );

~ Date ();

Void operator = (const Date &);

Void display () const ;};

Date: Date (int m, int d, int y)

{

Static char * mos [] =

{

"January", "February", "March", "Jun L", "May", "June ",

"July", "August", "September", "October", "November", "December "};

Mo = m; da = d; yr = y;

If (m! = 0)

{Month = new char [strlen (mos M-1]) + 1];

Strcpy (month, mos M-1]);

}

Else month = 0;

}

Date ::~ Date ()

{

Delete [] month;

}

Void Date: display ()

Const

{

If (month! = 0) cout month, '', da,", yr, endl;

}

Void Date: operator = (const Date & dt)

{

If (this! = & Dt)

{

Mo = dt.mo;

Da = dt. da;

Yr = dt. yr;

Delete [] month;

If (dt. month! = 0)

{

Month = new char

[Std: strlen (dt. month) + 1];

Std: strcpy (month, dt. month );

}

Else month = 0;

}

}

Int main ()

{

Date birthday (1979 );

Birthday. display ();

Date newday (2003 );

Newday. display ();

Newday = birthday;

Newday. display ();

Return 0;

}

In addition to adding an overload operator function to the Date class, this program is the same as the above program. The value assignment operator function first obtains the required data, and then uses delete to return the memory occupied by the original month pointer to the heap. Then, if the month pointer of the source object has been initialized, use the new operator to re-allocate the memory for the object and copy the month string of the source object to the receiver.

The first statement of the value assignment operator function of the reload Date class compares the address of the source object and the this pointer. This operation will not assign a value to itself.

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.