可以重载的运算符
![](https://i-blog.csdnimg.cn/blog_migrate/4e064d607dcfd027a2b01a09582f9a1d.png)
用法
返回类型+operator+运算符+(形参)+{操作}
eg:void* operator new(size_t size){操作}
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
class A
{
public:
A(int i = 0) :m_i(5) { cout << "A" << endl; }//A的构造函数
friend ostream& operator<<(ostream& os, A& a);//<<的重载
void* operator new(size_t size)//new的重载
{
cout << "new" << endl;
return malloc(size);
}
void operator delete(void * p)//delete的重载
{
cout << "delete" << endl;
return free(p);
}
private:
int m_i;
};
ostream& operator<<(ostream& os, A& a)//<<的重载
{
os << a.m_i;
return os;
}
void main()
{
A a;
cout << a << endl; //<<(cout,a)
A* p = new A(7);
delete p;
}
执行结果
![](https://i-blog.csdnimg.cn/blog_migrate/760f6be084987bc83c3f91d6d6657fc6.png)