什么是运算符重载
运算符重载:用同一个运算符完成不同的运算功能。
C++运算符重载的相关规定如下:
1.不能改变运算符的优先级。
2.不能改变运算符的结合性。
3.默认参数不能和重载的运算符一起使用,也就是说,在设计运算符重载成员函数时不能使用默认函数。
4.不能改变运算符的操作数的个数。
5.不能创建新的运算符,只有已有运算符可以被重载
6.运算符作用于C++内部提供的数据类型时,原来含义保持不变
相关代码
- 关系运算符重载
#include <iostream>
using namespace std;
class Girl
{
private:
int age;
int figure;
int yanzhi;
public:
Girl(int a,int b,int c):age(a),figure(b),yanzhi(c)
{}
int Sum(const Girl& g1)
{
return age+figure+yanzhi;
}
bool operator < (const Girl& g1)
{
if(Sum(g1)<Sum(*this))
{
return true;
}
return false;
}
bool operator == (const Girl& g1)
{
if(Sum(g1)==Sum(*this))
{
return true;
}
return false;
}
bool operator >(const Girl& g1)
{
if(Sum(g1)>Sum(*this))
{
return true;
}
return false;
}
};
int main()
{
Girl g1(18,90,80),g2(17,80,80);
if(g1<g2)
{
cout<<"g1 is better"<<endl;
}
if(g1==g2)
{
cout<<"g1 is equal to g2"<<endl;
}
if(g1>g2)
{
cout<<"g1 is better"<<endl;
}
return 0;
}
- 左移运算符
注意::左移运算符的重载函数不能作为成员函数,如果想调用类的私有成员需要使用友元函数
#include <iostream>
using namespace std;
class Girl
{
private:
int age;
string sex;
string name;
public:
Girl(int a,string b,string c):age(a),sex(b),name(c)
{}
friend ostream& operator <<(ostream& os,const Girl& g);//重载左移运算符
};
ostream& operator <<(ostream& os,const Girl& g)
{
cout<<g.age<<g.sex<<g.name;
return os;
}
int main()
{
Girl g(18,"女","小美");
cout<<g<<endl;
return 0;
}
- 下标运算符
#include <iostream>
#include <vector>
using namespace std;
class girl
{
private:
int age;
string sex;
string name;
vector<string> boys;
public:
girl(int a,string s,string n,vector<string> b)
{
age=a;
sex=s;
name=n;
boys=b;
}
void operator[](int i)
{
if(i>boys.size()-1)
{
cout<<"error"<<endl;
return;
}
cout<<boys[i]<<endl;
}
};
int main()
{
girl g(18,"girl","xiaohong",{"xiaoming","xiaohong"});
g[0];
g[1];
g[2];//error
return 0;
}
- 赋值运算符
注意: 系统自带的默认赋值函数是浅拷贝,如果类中成员有使用了堆区内存则需要自己重载赋值运算符
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
class Girl
{
public:
int age;
string sex;
string name;
int* m_ptr;
Girl()
{
m_ptr = nullptr;
}
//重载赋值运算符
Girl& operator=(const Girl& g)
{
if(this==&g)
{
return *this;
}
if(g.m_ptr==nullptr)
{
if(m_ptr!=nullptr)
{
delete m_ptr;
m_ptr = nullptr;
}
}
else
{
if(m_ptr==nullptr)
{
m_ptr = new int;
}
memcpy(g.m_ptr, m_ptr, sizeof(int));
}
age=g.age;
sex=g.sex;
name=g.name;
return *this;
}
void print()
{
cout<<"age:"<<age<<endl;
cout<<"sex:"<<sex<<endl;
cout<<"name:"<<name<<endl;
cout<<"*m_ptr:"<<*m_ptr<<endl;
cout<<"m_ptr:"<<m_ptr<<endl;
cout<<endl;
}
};
int main()
{
Girl g1;
g1.age=18;
g1.sex="girl";
g1.name="xiaohong";
g1.m_ptr = new int(100);
g1.print();
Girl g2;
g2=g1;
g2.print();
return 0;
}
- 內存分配符(new和delete)
注意:这里虽然没有使用static
关键字,但是这里对new
和delete
的重载函数属于类的静态成员函数,它并不能访问类的非静态成员。
#include <iostream>
using namespace std;
class girl
{
private:
int age;
int bh;
public:
girl(int a, int b) : age(a), bh(b)
{
cout << "构造函数" << endl;
}
~girl()
{
cout << "析构函数" << endl;
}
void *operator new(size_t size)
{
cout << "operator new" << endl;
return malloc(size);
}
void operator delete(void *p)
{
cout << "operator delete" << endl;
free(p);
}
};
int main()
{
girl *p = new girl(18, 170);
delete p;
return 0;
}