运算符重载
基本概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。
加号运算符重载
作用:实现两个自定义数据类型相加的运算
class Person {
public :
Person() {};//无参构造函数
Person(int a, int b)
{
this->A = a;
this->B = b;
}
/*Person operator+(const Person & p)
{
Person temp;
temp.A = this->A + p.A;
temp.B = this->B + p.B;
return temp;
}*/
/*
本质 Person p3=p1.operate+(p2)
*/
int A, B;
};
//全局函数实现 + 号运算符重载
Person operator+(Person p1, Person p2)
{
Person temp;
temp.A = p1.A + p2.A;
temp.B = p1.B+ p2.B;
return temp;
}
/*
本质 Person p3=operate+(p1,p2)
*/
//运算符重载 可以发生函数重载
Person operator+(const Person& p2, int v)
{
Person temp;
temp.A = p2.A + v;
temp.B = p2.B + v;
return temp;
}
void test01()
{
Person p1;
p1.A = 10;
p1.B = 20;
Person p2(10,30);
Person p3 = p1 + p2;
cout << p3.A << " " << p3.B;
}
左移运算符重载
作用:可以输出自定义数据类型
//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p)
{//本質:operator<<(cout ,p) 简化cout<<
out << "a:" << p.A << " b:" << p.B;
return out;
}
但是如果我想访问一下类内的私有成员怎么办呢?
很简单,只需要在类中把重载的左移运算符设置为友元。
friend ostream& operator<<(ostream& out, Person& p);
递增运算符重载
作用: 通过重载递增和递减运算符,实现自己的整型数据
class MyInteger {
friend ostream& operator<<(ostream& out, MyInteger myint);
public:
MyInteger()//无参构造函数
{
Num = 0;
}
//前置++
MyInteger& operator++()
{
Num++;
return *this;
}
MyInteger& operator++(int )
{
MyInteger temp = *this;//记录当前本身的值,然后让本身的值加1,
//但是返回的是以前的值,达到先返回后++
Num++;
return temp;
}
//前置--
MyInteger& operator--()
{
Num--;
return *this;
}
MyInteger& operator--(int)
{
MyInteger temp = *this;//记录当前本身的值,然后让本身的值加1,
//但是返回的是以前的值,达到先返回后++
Num--;
return temp;
}
private:
int Num;
};
ostream& operator<<(ostream& out, MyInteger myint)
{//本質:operator<<(cout ,p) 简化cout<<
out << "myint.Num=" << myint.Num;
return out;
}
void test03()
{
MyInteger myInt;
cout << myInt << endl;
cout << ++myInt << endl;
cout << myInt << endl;
cout << myInt-- << endl;
cout << myInt << endl;
cout << --myInt << endl;
cout << myInt << endl;
}
赋值运算符重载
c++编译器至少给一个类添加4个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
- 赋值运算符 operator=, 对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
class Person
{
public:
Person(int age)
{
//将年龄数据开辟到堆区
Age = new int(age);
}
//编译器提供的代码是浅拷贝
//m_Age = p.m_Age;
//提供深拷贝 解决浅拷贝的问题
Person& operator=(Person& p)
{
if (Age!=NULL)
{
delete Age;
}
//编译器提供的代码是浅拷贝
//m_Age = p.m_Age;
//提供深拷贝,解决深拷贝的问题
Age = new int(*p.Age);
//返回自身
return *this;
}
~Person()
{
if (Age != NULL)
{
delete Age;
Age = NULL;
}
}
int* Age;
};
void test04()
{
Person p1(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1; //赋值操作
cout << "p1的年龄为:" << *p1.Age << endl;
cout << "p2的年龄为:" << *p2.Age << endl;
cout << "p3的年龄为:" << *p3.Age << endl;
}
关系运算符重载
作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
class Person
{
public:
Person(int age,string name):Name(name)
{
//将年龄数据开辟到堆区
Age = new int(age);
}
bool operator==(Person& p)
{
if ((this->Name == p.Name) && (*(this->Age) == *(p.Age)))
{
return true;
}
else
{
return false;
}
}
bool operator!=(Person& p)
{
if ((this->Name == p.Name) && (*(this->Age) == *(p.Age)))
{
return false;
}
else
{
return true;
}
}
//编译器提供的代码是浅拷贝
//m_Age = p.m_Age;
//提供深拷贝 解决浅拷贝的问题
Person& operator=(Person& p)
{
Name = p.Name;
if (Age != NULL)
{
delete Age;
}
//编译器提供的代码是浅拷贝
//m_Age = p.m_Age;
//提供深拷贝,解决深拷贝的问题
Age = new int(*p.Age);
//返回自身
return *this;
}
~Person()
{
if (Age != NULL)
{
delete Age;
Age = NULL;
}
}
int *Age;
string Name;
};
void test05()
{
Person a(18,"张三");
Person b(18,"张三");
Person c(20, "李四");
if (a == b)
{
cout << "a和b相等" << endl;
}
else
{
cout << "a和b不相等" << endl;
}
if (a != c)
{
cout << "a和c不相等" << endl;
}
else
{
cout << "a和c相等" << endl;
}
}
函数调用运算符重载
- 函数调用运算符 () 也可以重载
- 由于重载后使用的方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
class Myprint {
public :
void operator()(string txt)
{
cout << "txt: " << txt <<endl;
}
};
class MyAdd
{
public:
int operator()(int v1, int v2)
{
return v1 + v2;
}
};
void test06()
{
//重载的()操作符 也称为仿函数
Myprint myFunc;
myFunc("hello world");
MyAdd add;
int ret = add(10, 10);
cout << "ret = " << ret << endl;
//匿名对象调用
cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}