C + + operator overloading, output symbol overloading

Source: Internet
Author: User

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

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.