C++ 运算符重载

一、成员函数实现运算符功能(不重载)

class Person {
public:
	int age;
	Person() : age(18) {}

	Person add(const Person& p) {
		Person temp;
		temp.age = this->age + p.age;
		return temp;
	}
};

调用方式:Person p3 = p1.add(p2);

add函数中的参数加个&的优点(不考):

  1. 节约程序运行时间。
  2. 节约内存,避免调用拷贝构造函数。如果去掉&,就会调用拷贝构造函数,多占一份内存。

前面加个const是为了避免被修改。

二、成员函数重载运算符

1、重载"+"

class Person {
public:
	int age;
	Person() : age(18) {}
	
Person operator+(const Person& p) {
		Person temp;
		temp.age = age + p.age;
		return temp;
	}
}

函数名是关键字 operator 和其后要重载的运算符符号构成。

调用方式:Person p3 = p1 + p2;(等价于p1.operator+(p2))

2、重载流插入运算符"<<"

class Person {
public:
	int id;
	int age;
	Person() : id(0), age(18) {}

	void operator<<(ostream& cout) {
		cout << id << "," << age;
	}
};

调用方式:p << cout;(等价于p.operator<<(cout);)

注意这里cout只能在右边,因为这里是在person类里面的成员函数,所以p必须在左边。如果要让p在右边,那么只能使用下面的全局函数。

3、重载关系运算符

class Person {
public:
	int id;
	int age;

	Person(int _id, int _age) : id(_id), age(_age) {}
	//带参数的构造函数
	bool operator==(const Person& p) {
		return (id==p.id && age==p.age);
	}
	bool operator!=(const Person& p) {
		return (id != p.id || age != p.age);
	}
};

调用方式:p1 == p2;,p1 != p2;

4、重载自增运算符"++"(前缀)

class Person {
public:
	int age;
	Person() : age(18) {}

	Person& operator++() {
		++age;
		return *this;
	}
};
ostream& operator<<(ostream& cout, const Person& p) {
	cout << p.age;
	return cout;
}

调用方式:cout << ++(++p) << endl;

注意这里如果把person&中的&去掉,则++p会创建一个新的person对象,这个新的对象与原来的对象p没有任何关系,所以当进行cout << ++(++p) << endl;之后再进行cout << p << endl;,得到的是两个不同的值(p只自加了一次)。

5、重载自增运算符"++"(后缀)

注意不支持(a++)++

class Person {
public:
	int age;
	Person() : age(18) {}

	Person operator++(int) {
		Person temp = *this;
		++age;
		return temp;
	}
};
ostream& operator<<(ostream& cout, const Person& p) {
	cout << p.age;
	return cout;
}

int来区分++aa++

调用方式:cout << p++ << endl;

6、重载"="

class Person {
public:
	int* age;

	Person(int _age) : age(new int(_age)) {}
	~Person() {
		delete age;
	}

注意new要和delete配对,否则有可能出现内存溢出。(重点)

如果创建两个对象p1,p2Person p1(18); Person p2(28);然后使p2=p1;(调用的是拷贝构造函数,浅拷贝),则会报错,因为p2指向了p1的18,导致p1析构了两次,从而出错,下面来看深拷贝。

class Person {
public:
	int* age;

	Person(int _age) : age(new int(_age)) {}
	~Person() {
		delete age;
	}

	Person& operator=(Person& p) {
		age = new int(*p.age);
		return *this;
	}
};

这时如果p2=p1;则调用的是"="重载函数,而Person p3 = p1;调用的依然是拷贝构造函数。

但这个时候如果有p2=p1;,其实存在内存泄露,因为这个时候p2创建了一个新的18,而原来的28没有被删除。所以要把重载函数改成下面这种形式。

Person& operator=(Person& p) {
		if (age != NULL) {
			delete age;
		}

		age = new int(*p.age);
		return *this;
	}

这个时候就把原来的28delete了然后创建了一个新的18。(重点)

这里也可以实现链式调用p3=p2=p1(不考)

7、仿函数(重载函数调用运算符 “()”)

class Adder {
public:
	int operator()(int a, int b) {
		return a + b;
	}
};

在主函数中调用如下:

void main() {
	Adder adder;
	cout << adder(2, 3) << endl;
}

外观上看起来就是一个函数声明+函数调用。(可能考可能不考)

三、全局函数重载运算符

1、重载"+"

class Person {
public:
	int age;
	Person() : age(18) {}
};

Person operator+(const Person& p1, const Person& p2) {
	Person temp;
	temp.age = p1.age + p2.age;
	return temp;
}

调用方式:Person p3 = p1 + p2;(等价于operator+(p1,p2))

2、重载流插入运算符"<<"

class Person {
public:
	int id;
	int age;

	Person() : id(0), age(18) {}
}void operator<<(ostream& cout, const Person& p) {
	cout << p.id << "," << p.age;
}

调用方式:cout << p;等价于operator<<(cout, p);

若将参数调换位置依然是p << cout;

但是这种方式不能实现cout << p1 << p2;,下面来看另一种链式编程的实现方式。(必考)

class Person {
public:
	int id;
	int age;
	Person() : id(0), age(18) {}
};
ostream& operator<<(ostream& cout, const Person& p) {
	cout << p.id << "," << p.age;
	return cout;
}

调用方式:cout << p1 << p2;(等价于operator<<(operator<<(cout, p), p1);)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值