目录
对已有的运算符重新进行定义,赋予其另一种功能,以适应自定义的数据类型;
运算符重载的关键字operator。
1,加号运算符重载
实现两个自定义的数据类型相加的运算。
有两种重载方法:使用成员函数,使用全局函数;
使用成员函数对+重载:
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
Person operator+(Person p)
{
Person temp;
temp.m_A = this->m_A + p.m_A;
temp.m_B = this->m_B + p.m_B;
return temp;
}
int m_A;
int m_B;
};
void test()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
cout << p3.m_A << endl;
cout << p3.m_B << endl;
}
int main()
{
test();
system("pause");
return 0;
}
注意:用成员函数对+重载,自定义类型进行相加操作时的原型如下,即为函数的调用;
Person p3 = p1.operator+(p2);
Person p3 = p1 + p2;
使用全局函数对+重载:
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
int m_A;
int m_B;
};
Person operator+(Person p1,Person p2)
{
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
void test()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p2;
p2.m_A = 10;
p2.m_B = 10;
Person p3 = p1 + p2;
cout << p3.m_A << endl;
cout << p3.m_B << endl;
}
int main()
{
test();
system("pause");
return 0;
}
注意:用全局函数对+重载,自定义类型进行相加操作时的原型如下,即为函数的调用;
Person p3 = operator+(p1,p2);
Person p3 = p1 + p2;
如果一个自定义类型和固有数据类型相加时怎么操作呢?即p1+10(Person类+int类),需要对operator+函数进行重载;
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
int m_A;
int m_B;
};
Person operator+(Person p1,Person p2)
{
Person temp;
temp.m_A = p1.m_A + p2.m_A;
temp.m_B = p1.m_B + p2.m_B;
return temp;
}
Person operator+(Person p1, int num)
{
Person temp;
temp.m_A = p1.m_A + num;
temp.m_B = p1.m_B + num;
return temp;
}
void test()
{
Person p1;
p1.m_A = 10;
p1.m_B = 10;
Person p3 = p1 + 100;
cout << p3.m_A << endl;
cout << p3.m_B << endl;
}
int main()
{
test();
system("pause");
return 0;
}
上边代码是对+运算符的重载,同样对于减号、乘号、除号等运算符重载可使用同样的方法。
operator为关键字;
注意:
对于内置的数据类型的表达式的运算符是不可能改变其特性的;
不要滥用运算符重载,例如不要把加号重载为减的逻辑。
2,左移运算符重载
#include <iostream>
using namespace std;
#include <string>
class Person
{
friend ostream& operator<<(ostream& cout, Person p);
public:
Person()
{
m_A = 100;
m_B = 200;
}
private:
int m_A;
int m_B;
};
ostream& operator<<(ostream& cout, Person p)
{
cout << "m_A=" << p.m_A << " " << "m_B=" << p.m_B << endl;
return cout;
}
void test()
{
Person p1;
cout << p1;
}
int main()
{
test();
system("pause");
return 0;
}
运行结果:
m_A=100 m_B=200
请按任意键继续. . .
注意:
在对<<运算符重载时,需要使用全局函数,才能实现cout<<p这种形式的调用;
cout的类型是ostream,对<<运算符重载的函数的返回值将cout返回(返回类型是ostream&)类型才能实现链式编程;
当自定义类型中存在private属性时,运算符重载函数需要设定为这个自定义类型的友元;
cout的类型为ostream&,因为需要使用同一个cout;
3,递增运算符重载
#include <iostream>
using namespace std;
#include <string>
class Person
{
friend ostream& operator<<(ostream& cout, Person p);
public:
Person& operator++()
{
m_A = m_A + 1;
return *this;
}
Person operator++(int)
{
Person temp = *this;
m_A = m_A + 1;
return temp;
}
Person()
{
m_A = 100;
}
private:
int m_A;
};
ostream& operator<<(ostream& cout, Person p)
{
cout << "m_A=" << p.m_A << endl;
return cout;
}
void test()
{
Person p1;
cout << ++(++p1);
Person p2;
cout << (p2++)++;
}
int main()
{
test();
system("pause");
return 0;
}
注意:
++分前置和后置,需要对operator++函数进行重载,在operator++(int)增加占位参数,编译器就能自动识别为后置++;
前置++重载函数的返回值类型是引用,函数返回的是对象本身,需要一直指向对象本身;
后置++重载函数的返回值类型是值,因为后置++返回的是局部变量,如果返回引用就会在函数运行完成后丢失这个引用所指向的对象;
4,赋值运算符重载
我们知道C++编译器至少给一个类添加四个函数:
默认构造函数(无参,函数体为空);
默认析构函数(无参,函数体为空);
默认拷贝构造函数,对属性进行值拷贝;
赋值运算符operator=,对属性进行值拷贝;
我们看到默认拷贝构造函数和赋值运算符operator=都是进行属性的值拷贝,属于浅拷贝,如果存在堆区的数据,就会涉及到深拷贝,就需要对默认的构造函数进行重写。
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
Person(int num)
{
m_A = new int(num);
}
~Person()
{
if (m_A != NULL)
{
delete m_A;
m_A = NULL;
}
}
Person& operator=(Person &p)
{
if (m_A != NULL)
{
delete m_A;
m_A = NULL;
}
m_A = new int(*p.m_A);
return *this;
}
int* m_A;
};
void test()
{
Person p1(18);
Person p2(20);
p2 = p1;
cout << *p2.m_A << endl;
}
int main()
{
test();
system("pause");
return 0;
}
注意:
=重载函数的形参类型为引用类型;
=的重载与自定义的拷贝构造函数类似,我们知道默认的拷贝构造函数是对值的浅拷贝,如果有存放在堆区的数据,就需要进行深拷贝,=的重载本质上属于深拷贝。
5,关系运算符重载
#include <iostream>
using namespace std;
#include <string>
class Person
{
public:
Person(int num,string str)
{
m_A = num;
m_B = str;
}
bool operator==(Person p)
{
if (this->m_A == p.m_A && this->m_B == p.m_B)
{
return true;
}
return false;
}
int m_A;
string m_B;
};
void test()
{
Person p1(18,"Tom");
Person p2(20,"Jame");
if (p1==p2)
cout <<"p1与p2相同" << endl;
else
cout << "p1与p2不相同" << endl;
}
int main()
{
test();
system("pause");
return 0;
}
同样的,其他关系运算符也可按此实例进行重载。
6,函数调用运算符重载
函数调用运算符()也可以重载;
由于重载后使用的方式非常像函数的调用,因此称为仿函数;
仿函数没有固定的写法,非常灵活;
#include <iostream>
using namespace std;
#include <string>
class Add
{
public:
int operator()(int a, int b)
{
return a + b;
}
};
void test()
{
Add myAdd;
cout << myAdd(10, 20) << endl;
cout << Add()(30, 40) << endl;
}
int main()
{
test();
system("pause");
return 0;
}
注意:
()重载后使用可以有两种方法,第一种:创建一个对象,使用这个对象进行使用,如代码中的myAdd(10, 20);第二种:可以通过匿名对象使用,Add()(30, 40)。