Operators have single-and binocular operators, followed by one operand and two operands, respectively. In C + + either way they can manipulate multiple types of variables, such as the + operator a+b,a,b can be either integer or float (float), the compiler has a symbol table to save the type of a, B (you can see the compiler principle), when the compiler is translating A + b calls the corresponding subroutine processing according to its type. Sometimes we think that the type B is not enough, this time, we can overload +, for example, we want to implement CLASSA+CLASSB, we can overload +, return value operator+ (ClassA a,classb& b) {
What to do .....
return value
}
Exactly what to do and what the return value is defined by itself. The output symbol "How to overload" is given below.
#include "stdafx.h"
#include <iostream>
using namespace Std;
Class g{
Public
int m;
};
ostream& operator<< (G h,ostream& o) {//h left operand, O is the right operand of
Return o<
} ;
int _tmain (int argc, _tchar* argv[])
{
G K;
k.m=100;
k<<cout<<endl;
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + operator overloading, output symbol overloading