NetEase Cloud Classroom _c++ Development Introduction to Mastery _ Chapter 4: operator overloading

Source: Internet
Author: User

Lesson 23 Operator Overloading

Operator overloading

Overloaded assignment operators

person& person::operator= (const person& Other) {//Check self-assignment if (this = = &other) {return *this;} Release the original memory resource delete[]m_data;int length = strlen (other.m_data); m_data = new Char[length + 1];strcpy (m_data, Other.m_data) ;//Return the reference to this object return *this;}

Lesson 25 self-increment operator overloading

Operator overloading

Difference between pre-increment and post-increment

Incremental modification of an object (operand) when using the pre-increment, and then returning the object

When using post-increment, you must return the original object value before the increment

Class increase{public:increase (int x): value (x) {}increase& operator + + ();//pre-increment increase operator + + (int);// After increment private:int value;};

Overloads of the member form of + +

increase& Increase::operator + + ()//pre-increment {value++;//First increment return *this;//return an object}increase Increase::operator + + (int)// After increment {increase temp (*this);//Temporary object holds original object value value++;//original object increment change return temp;//return original object value}

NetEase Cloud Classroom _c++ Development Introduction to Mastery _ Chapter 4: 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.