在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;
}