For simple operators, you can refer to the previous blog post. You will then have a blog post that studies the dark copy from the perspective of overloading the equals operator.
Comma operator overloading
The comma operator overload requires a parameter and returns its own class. The comma operator is common in copy operations, and the following is the comma operator overload, which is an example of an assignment operation.
#include<string>#include<iostream>using namespace std;class Tem{ private: int x; public: Tem(int); Tem operator ,(Tem); void display();}; Tem::Tem(int xx=0){ x = xx;}Tem Tem::operator , (Tem t){ cout<<t.x<<endl; return Tem(t.x);}void Tem::display(){ cout<<"Class(Tem) includes of: "<<x<<endl;}int main(){ Tem tem1(10); Tem tem2(20); Tem other = (tem1,tem2); //会选择第二个 int a= 1,2;a等于2而不是1 other.display(); return 0;}
Take member operator overloading
Returns a pointer variable of the class type, in accordance with the usual usage, so that you can not use the pointer when declaring the variable, but can then be invoked as a pointer, simple and convenient.
#include<iostream>using namespace std;class Tem{ public: int n; float m; Tem* operator ->(){ return this; }}; int main(){ Tem tem; tem->m = 10; //调用->运算符,返回Tem*类型并访问m cout<<"tem.m is "<<tem.m<<endl; cout<<"tem->m is "<<tem->m<<endl; return 0;}
Input-output operator overloading
The >>,\<\< operator overloads are called after Cin, cout, respectively. We need to overload them with the meta operator, noting that the return types are IStream and Ostream, respectively.
#include <iostream>using namespace Std;class date{private:int year,month,day; public:date (int y,int m,int d) {year = y; month = m; Day = D; } Friend ostream& operator << (ostream &stream,const Date &date) {Stream<<date.ye ar<< "" <<date.month<< "" <<date.day<<endl; return stream; } Friend istream& operator >> (IStream &stream,date &date) {stream>>date.year> >date.month>>date.day; return stream; }}; int main () {Date _date (2017,6,13); cout<<_date; cin>>_date; cout<<_date; return 0;}
Welcome to further Exchange this blog post related content:
Blog Park Address: http://www.cnblogs.com/AsuraDong/
CSDN Address: Http://blog.csdn.net/asuradong
You can also write to communicate: [email protected]
Welcome reprint , but please specify the source:)
Operator overloading in-depth C + +