C + + operator overloading

Source: Internet
Author: User

#include "stdafx.h"
Class a{
Public
int test;
A (int j=0) {
Test=j;
}
Operator overloading basic Format return value + keyword +opereator+ reload symbol + () {}
Essentially, this is exactly like a function, except that the function name is changed to Opreator
is essentially a member function or a rule that waits for a member function
int operator << (int j) {
Return J;
}
A operator + (a j) {
A C;
c.test=this->test+j.test;
return C;
}
A minimum of two parameters and also a class object parameter or enum type
Friend int operator< (A a,int J);
};
int operator< (int j,a A) {
return a.test+j;
}

Overloading is generally a friend and member function two ways
int _tmain (int argc, _tchar* argv[])
{
How to call overloaded functions of course it's a clumsy way to call.
A (3), B (3);
int j=a.operator<< (1);
printf ("The value of the member function is:%d\n", j);

Let's look at a list of overloads
A c=a.operator+ (b);
printf ("In Therory c.test are 6:c.test is%d\n", c.test);

The call of the friend function can only take the parameter 1 + operator + parameter 2 to call order can not be wrong three purposes in order to three purposes
int p=1< (a);
printf ("In Theroy P was 4:p is%d\n", p);

The core idea of a relatively simple call method is to remove the. Opereator () in other words, the object + operator + parameter mode
A m=a+b;


while (1);
return 0;
}
/*

End it. (1) Operator overloading generally has only two forms, member overloading, friend function overloading
(2) The invocation of an operator overloaded function has two kinds of 1 for a member function: Calling 2 by a member function: invoking by a simple method
The friend function is a pattern call with a parameter + operator + parameter

*/

C + + operator overloading

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.