C + + operator overloading three (chained programming)

Source: Internet
Author: User

//chained programming of operator overloading#include <iostream>using namespacestd;//the friend function overloading operator only applies to scenarios where the left operand is a system variable//Because members cannot add class member functions to system variables, only by global functionsthe essence of the chain programming is that the function returns the worth left-hand value
classpoint{ Public: //Note in the friend function, the return value is different, the friend function will be different, and the function overload has a differenceFriend Ostream &operator<< (Ostream & out, point&pin); Point (intXinty) { This->x =x; This->y =y; } Point&p) { This->x =p.x; This->y =p.y; cout<<"the copy constructor was executed 1"<<Endl; } ~Point () {cout<<"destructors have been executed for 2"<<Endl; } voidPRINTFA () {cout<<"x="<< This->x <<Endl; cout<<"y="<< This->y <<Endl; } Private: intx; inty;};//first version (non-chained programming)/*void operator<< (ostream &out, point& pin) {out << "x=" << pin.x << Endl; Out << "y=" << pin.y << Endl;}*///second version (chained programming)Ostream &operator<< (Ostream & out, point&pin) { out<<"x="<< pin.x <<Endl; out<<"y="<< PIN.Y <<Endl; return out;}voidProtecta () {point P1 (5,5); //Requirement: We need to print this class object, and the system cannot print the custom class, then we must overload the << operator//for a two-tuple operator, it is also critical to add a point and the position of two operands //For example: cout << P1, left operand is cout, right operand is P1, if write class member function then must cout.operator<< (p1)//Here we obviously cannot add operator overloads to the class that corresponds to the Cout object, only to make the UF meta-functioncout <<P1; //Step 1: First admit that operator overloading is a function that writes out the name of a functor//operator<< ()//Step 2: Write out the parameter list according to the operand//operator<< (COUT,P1)//Step 3: Return values based on business completion functions, and implement functions//the type of void operator<< (COUT,P1) cout is Ostream//The introduction of chained programming is now starting//for cout << p1<< "Asdfasdfas" <<endl; //cout << p1 << "Asdfasdfas" << Endl; Error C2296: "<<": illegal, left operand contains "void" type//This means that the void type has no overloaded operator <<, whereas the cout << P1 function should return the cout variable, then the output "Asdfasdfas" can be implemented//so the modification produces version twocout << P1 <<"Asdfasdfas"<<Endl;}voidMain () {protecta (); System ("Pause");}

C + + operator overloading three (chained programming)

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.