[C ++ basics 07] Operator Overloading and Operator Overloading

Source: Internet
Author: User

[C ++ basics 07] Operator Overloading and Operator Overloading
1. What is Operator overload?

As the name suggests, for example, the heavy-load operator +-*/changes the original meaning of these symbols.

C ++ provides the operator keyword, Which is used together with the operator to represent an operator function. operator = should be considered as a function name.

2. Two implementations

There are two methods to implement the overload of operators:

(1) Reload as a member function of the class

<Function return type> operator <operator> (<parameter table>) {<function body> ;}

(2) overload a friend function as a class

Friend <function return type> operator <operator> (<parameter table>) {<function body> ;}

3. Example

(1) Reload as a member function of the class

Class Test {public: Test (void): a (0 ){};~ Test (void) {}; Test (int _ a): a (_ a) {}; Test & operator + (Test & _ test) // overload operator, returns an instance of this class {int new_a = a + _ test. a; Test new_Test (new_a); return new_Test ;}; int a ;}; void main () {Test t1 (1), t2 (2), t3; t3 = t1 + t2; std: cout <t3.a; return ;}
(2) overload a friend function as a class

Class Test {public: Test (void): a (0 ){};~ Test (void) {}; Test (int _ a): a (_ a) {}; friend Test & operator + (Test & _ test1, Test & _ test2) // The overload is a friend function. Because the friend function is not a class member, you need to input two parameters {int new_a = _ test1.a + _ test2.a; Test new_Test (new_a ); return new_Test ;}; int a ;}; void main () {Test t1 (1), t2 (2), t3; t3 = t1 + t2; std :: cout <t3.a; return ;}


Note that when the operator is overloaded, it is best to reference Test instead of Test in the returned result. The reason is as follows:

(1) If a reference is returned, the return value is the returned copy (equivalent to calling the copy constructor multiple times, which slows down the efficiency)

(2)Problems may occur during continuous operations,For example, (a = B) = c, a = B returns a temporary object (copy). After (a = B) = c, c cannot assign a value to.







C language Operator Overloading

C ++

Class {
Public:
Int I, j;

Class A & operator + (class A a1, class A a2)
{I = a1. I + a2. I; j = a1.j + a2.j; return * this}

Class A & operator-(class A a1, class A a2)
{I = a1. I-a2. I; j = a2.j-a2.j; return * this}
}

C/C ++ Operator Overloading

This value is a Hidden Pointer of the class. What does it mean? That is to say, we do not write this pointer when defining classes, but such a pointer is automatically generated, which points to the object itself.

Therefore, * this is to dereference the pointer, which is equivalent to the object of the class itself.

If Clock;
* This is actually similar to.

& Indicates a reference. In this form, the object of the class is returned and no temporary variables are created.
And can be used as the left value.

The left value is placed on the left of = and assigned to it with other values.

 

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.