C++的运算符重载
来源:互联网 发布:mobi的域名含义 编辑:程序博客网 时间:2024/06/16 23:41
一元运算符重载
以++为例:
#include <iostream>#include <string>using namespace std;class Stu{ friend Stu& operator++();//前置++的友元函数重载 friend Stu operator++(Stu &stu, int);//后置++的友元函数重载public: Stu(int x, int y) { m_x = x; m_y = y; } //后置++的成员函数重载 //Stu operator++(int) //{ // Stu old(*this); // m_x++; // m_y++; // return old; //} //前置++的成员函数重载 /*Stu& operator++() { m_x = m_x+1; m_y = m_y+1; return *this; }*/ void print() { cout << m_x << " " << m_y<<endl; } int getX() { return m_x; } int getY() { return m_y; }private: int m_x; int m_y;};////Stu& operator++(Stu &stu)//{// stu.m_x = stu.m_x + 1;// stu.m_y =stu.m_y + 1;// return stu;//}/*Stu operator++(Stu &stu, int)//必须使用引用{ Stu old(stu); stu.m_x++; stu.m_y++; return old;}*/int main(){ Stu stu(2,6); //cout << (stu++).getX() << endl; //cout << (stu++).getX() << endl; return 0;}
二元运算符的重载
“+”的成员函数和友元函数的运算符重载
#include <iostream>#include <string>using namespace std;class Stu{ friend Stu& operator+(Stu &a, Stu &b);public: /*Stu &operator+(Stu &stu) { Stu stu0(0,0); stu0.m_x = this->m_x + stu.m_x; stu0.m_y = this->m_y + stu.m_y; return stu0; }*/ Stu(int x, int y) { m_x = x; m_y = y; } void print() { cout << m_x << " " << m_y<<endl; } int getX() { return m_x; } int getY() { return m_y; }private: int m_x; int m_y;};Stu& operator+(Stu &a, Stu &b){ Stu c(0, 0); c.m_x = a.m_x + b.m_x; c.m_y = a.m_y + b.m_y; return c;}int main(){ Stu stu1(2,6); Stu stu2(6,2); Stu stu0 = stu1 + stu2; cout << stu0.getX() << " " << stu0.getY() << endl; return 0;}
“<<”的运算符重载
注意:”<<”只能在友元函数中进行运算符重载,因为重载时的第一个参数必须是”ostream”类型的值。
#include <iostream>using namespace std;class Stu{ friend ostream& operator<<(ostream &out, Stu &stu);public: Stu(int x, int y) { m_x = x; m_y = y; } void print() { cout << m_x << " " << m_y<<endl; } int getX() { return m_x; } int getY() { return m_y; }private: int m_x; int m_y;};ostream& operator<<(ostream &out, Stu &stu) { out << stu.m_x << " " << stu.m_y << endl; return out;}int main(){ Stu stu(2,6); cout << stu << endl; return 0;}
索引”[ ]”的运算符重载
注意:索引运算符只能进行成员函数的运算符重载。
#include <iostream>using namespace std;class Stu{public: int operator[](int index) { if (index == 0) { return m_x; } else if (index == 1) { return m_y; } } Stu(int x, int y) { m_x = x; m_y = y; } void print() { cout << m_x << " " << m_y<<endl; } int getX() { return m_x; } int getY() { return m_y; }private: int m_x; int m_y;};int main(){ Stu stu(2,6); cout << stu[0] << endl; cout << stu[1] << endl; return 0;}
0 0
- c++-运算符的重载
- C++--运算符的重载
- [C/C++]运算符的重载
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- C#:运算符重载
- [C++]运算符重载
- 【c++】运算符重载
- app接口总结
- 19.显示购物车商品(没有使用sharedprefrene,使用了hasmap,还有加减控件不能用)
- RabbitMQ3.6.5集群搭建和遇到的问题
- CENTOS安装ElasticSearch
- error: Error retrieving parent for item: No resource found that matches the given name 'Theme.Ap
- C++的运算符重载
- OSSClient
- Java加密
- 第七周 oj 2013 一元二次方程求解
- iOS10推送必看UNNotificationServiceExtension
- 以下5点能做到3点,你的写作之路就会坚持很久
- Android-Universal-Image-Loader 使用遇到的oom问题
- 关于android 手机硬件加速问题
- 30.JDBC连接Oracle数据库的步骤