CPP(三)理解操作符重载:初步

在C++中我们将操作符看做包含前缀operator的函数。相信大家已经学会了操作符重载的一般方法。

这里要说的是,操作符重载是如何发生的。看下面的代码。

// Test.cpp : 定义控制台应用程序的入口点。
//
#include <iostream>
using namespace std;

class t
{
public:
	friend void operator|(const t& t1, const t& t2);
};
void operator|(const t& t1, const t& t2)
{
	cout << "调用的是类的友元函数" << endl;
}
int main(){
	t t1, t2;
	t1 | t2;
	return 0;
}

语句 t1 | t2 首先会被理解为: 

t1.operator|(t2);

可以看到,类似于一个普通的函数调用。但是t1并没有一个叫做 operator| 的成员函数,所以编译器会尝试第二种理解:

operator|(t1, t2);

编译器很欣喜地发现,这么理解没问题。

这里蕴含的知识点:

1、编译器对 使用操作符操作类的两种理解,以及两种理解的顺序问题。

2、操作符重载不是仅仅针对类成员函数,普通函数也可以重载操作符。

现在,下面这段代码的输出是什么您应该 很清楚了:

// Test.cpp : 定义控制台应用程序的入口点。
//
#include <iostream>
using namespace std;

class t
{
public:
	void operator|(const t& t1)
	{
		cout << "调用的是类的成员函数" << endl;
	}
	friend void operator|(const t& t1, const t& t2);
};
void operator|(const t& t1, const t& t2)
{
	cout << "调用的是类的友元函数" << endl;
}
int main(){
	t t1, t2;
	t1 | t2;
	return 0;
}


这里有一个常见应用:定义一个函数来重载<<操作符,使之可以输出您自定义的类。为了让这个函数可以访问自定义类,通常将这个函数定义为自定义类的友元函数。例如:

#include <iostream>
using namespace std;

class t
{
};
ostream& operator<<(ostream& os, const t& a)
{
	os << "输出类t" << endl;
	return os;
}
int main(){
	t t1, t2;
	cout << t1 << t2 << endl;
	return 0;
}

main中的cout行会被解释为:

operator<<( operator<<( operator<<(cout, t1), t2), endl)

这里的endl又是什么东西呢?下面的代码模拟了一个endl(就是代码中的fake_endl)。看了以后您应当就明白了。

#include <iostream>
using namespace std;

class t
{
};
typedef void(*FunctionType)(void);
ostream& operator<<(ostream& os, FunctionType func)
{
	os << "这里的代码完成输出换行" << endl;
	func();
	return os;
}
ostream& operator<<(ostream& os, const t& a)
{
	os << "输出类t" << endl;
	return os;
}
void fake_endl()
{
	cout << "这里的代码完成刷新输出缓冲区" << endl;
}
int main(){
	t t1, t2;
	cout << t1 << t2 << fake_endl;
	return 0;
}

 




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值