不要滥用运算符重载
内置类型不能使用运算符重载
+号运算符重载
#include <iostream>
#include "mathutil.hpp"
#include <string>
#include "People.hpp"
#include "Phone.hpp"
using namespace std;
class Fclass{
public:
int m_a;
int m_b;
// // 成员函数重载
// Fclass operator+(Fclass &f) {
// Fclass fClass(0,0);
// fClass.m_a = this->m_a + f.m_a;
// fClass.m_b = this->m_b + f.m_b;
// return fClass;
// }
Fclass(int a,int b):m_a(a),m_b(b){
}
};
// 全局函数重载
Fclass operator+(Fclass &f1, Fclass &f2) {
Fclass fClass(0,0);
fClass.m_a = f1.m_a + f2.m_a;
fClass.m_b = f1.m_b + f2.m_b;
return fClass;
}
Fclass operator+(Fclass &f1, int num) {
Fclass fClass(0,0);
fClass.m_a = f1.m_a + num;
fClass.m_b = f1.m_b + num;
return fClass;
}
void test41() {
Fclass fa(10, 21);
Fclass fb(21, 5);
Fclass f = fa + fb;
std::cout << f.m_a << f.m_b << std::endl;
// 成员函数重载本质调用
//Fclass f = fa operator+ fb;
//全局函数重载本质调用
//Fclass f = operator(fa+ fb);
Fclass f2 = f + 10;
std::cout << f2.m_a << f2.m_b << std::endl;
}
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
test41();
std::cout << "end!\n";
return 0;
}
左移运算符重载
//全局函数重载左移运算符 不能用成员函数
ostream& operator<<(ostream &cout,Fclass &f) {// 本质operator<<(cout,f) 简化 cout<<
cout << f.m_a << f.m_b;
return cout;
}
void test42() {
Fclass fa(10, 21);
cout << fa << endl;
}
递增运算符重载
前置递增返回的是引用
后置递增返回的是值 后置递增后边需要一个int占位参数 必须是int类型
class Fclass{
public:
int m_a;
int m_b;
// 前置递增
Fclass& operator++(){// 返回引用是对同一个对象做操作
m_a++;
m_b++;
return *this;
}
// 后置递增
Fclass operator++(int) {
Fclass temp = *this;
m_a++;
m_b++;
return temp;
}
Fclass(int a,int b):m_a(a),m_b(b){
}
};
void test43() {
Fclass fa(10, 21);
cout << ++fa << endl;
Fclass fa1(10, 21);
fa1++;
cout<< fa1 << endl;
}
赋值运算符重载
#include <iostream>
using namespace std;
class GClass {
public:
int *m_a;
GClass& operator=(GClass &g){
//堆上有内存先释放
if (m_a != nullptr) {
delete m_a;
}
/// 深拷贝
m_a = new int(*g.m_a);
return *this;
}
GClass(int a){
m_a = new int(a);
}
~GClass() {
if (m_a != nullptr) {
delete m_a;
}
}
};
void test50() {
GClass g(10);
GClass g1(20);
GClass g2(30);
g = g1 = g2;
std::cout << *g.m_a << *g1.m_a << *g2.m_a << endl;
}
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
test50();
std::cout << "end!\n";
return 0;
}
关系运算符重载
class GClass {
public:
int *m_a;
bool operator==(GClass &g) {
if (*m_a == *g.m_a) {
return true;
}
return false;
}
bool operator!=(GClass &g) {
if (*m_a != *g.m_a) {
return true;
}
return false;
}
GClass(int a){
m_a = new int(a);
}
~GClass() {
if (m_a != nullptr) {
delete m_a;
}
}
};
函数调用运算符重载
#include <iostream>
#include <string>
using namespace std;
class NSLog{
public:
void operator()(string test) {
cout << test << endl;
}
};
class Add{
public:
int operator()(int a,int b) {
return a + b;
}
};
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
NSLog log;
log("hello world");// 使用起来和函数调用很像 因此称为仿函数 仿函数非常灵活没有固定的写法
Add add;
cout << add(209,111) << endl;
std::cout << "end!\n";
return 0;
}