C++运算符重载(友元函数方式)
2015-07-10 11:27
302 查看
我们知道,C++中的运算符重载有两种形式:①重载为类的成员函数(见C++运算符重载(成员函数方式)),②重载为类的友元函数。
当重载友元函数时,将没有隐含的参数this指针。这样,对双目运算符,友元函数有2个参数,对单目运算符,友元函数有一个参数。但是,有些运行符不能重载为友元函数,它们是:=,(),[]和->。
重载为友元函数的运算符重载函数的定义格式如下:
friend 函数类型 operator 运算符(形参表)
{
函数体;
}
#include <iostream.h>
class complex //复数类
{
public:
complex(){ real = imag = 0;}
complex(double r, double i)
{
real = r;
imag = i;
}
friend complex operator + (const complex &c1, const complex &c2); //相比于成员函数方式,友元函数前面加friend,形参多一个,去掉类域
friend complex operator - (const complex &c1, const complex &c2); //成员函数方式有隐含参数,友元函数方式无隐含参数
friend complex operator * (const complex &c1, const complex &c2);
friend complex operator / (const complex &c1, const complex &c2);
friend void print(const complex &c); //友元函数
private:
double real; //实部
double imag; //虚部
};
complex operator + (const complex &c1, const complex &c2)
{
return complex(c1.real + c2.real, c1.imag + c2.imag);
}
complex operator - (const complex &c1, const complex &c2)
{
return complex(c1.real - c2.real, c1.imag - c2.imag);
}
complex operator * (const complex &c1, const complex &c2)
{
return complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.real + c1.imag * c2.imag);
}
complex operator / (const complex &c1, const complex &c2)
{
return complex( (c1.real * c2.real + c1.imag * c2. imag) / (c2.real * c2.real + c2.imag * c2.imag),
(c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag) );
}
void print(const complex &c)
{
if(c.imag < 0)
cout<<c.real<<c.imag<<'i'<<endl;
else
cout<<c.real<<'+'<<c.imag<<'i'<<endl;
}
int main()
{
complex c1(2.0, 3.5), c2(6.7, 9.8), c3;
c3 = c1 + c2;
cout<<"c1 + c2 = ";
print(c3); //友元函数不是成员函数,只能采用普通函数调用方式,不能通过类的对象调用
c3 = c1 - c2;
cout<<"c1 - c2 = ";
print(c3);
c3 = c1 * c2;
cout<<"c1 * c2 = ";
print(c3);
c3 = c1 / c2;
cout<<"c1 / c2 = ";
print(c3);
return 0;
}
从运行结果上我们就可以看出来,无论是通过成员函数方式还是采用友元函数方式,其实现的功能都是一样的,都是重载运算符,扩充其功能,使之能够应用于用户定义类型的计算中。
当重载友元函数时,将没有隐含的参数this指针。这样,对双目运算符,友元函数有2个参数,对单目运算符,友元函数有一个参数。但是,有些运行符不能重载为友元函数,它们是:=,(),[]和->。
重载为友元函数的运算符重载函数的定义格式如下:
friend 函数类型 operator 运算符(形参表)
{
函数体;
}
一、程序实例
//运算符重载:友元函数方式#include <iostream.h>
class complex //复数类
{
public:
complex(){ real = imag = 0;}
complex(double r, double i)
{
real = r;
imag = i;
}
friend complex operator + (const complex &c1, const complex &c2); //相比于成员函数方式,友元函数前面加friend,形参多一个,去掉类域
friend complex operator - (const complex &c1, const complex &c2); //成员函数方式有隐含参数,友元函数方式无隐含参数
friend complex operator * (const complex &c1, const complex &c2);
friend complex operator / (const complex &c1, const complex &c2);
friend void print(const complex &c); //友元函数
private:
double real; //实部
double imag; //虚部
};
complex operator + (const complex &c1, const complex &c2)
{
return complex(c1.real + c2.real, c1.imag + c2.imag);
}
complex operator - (const complex &c1, const complex &c2)
{
return complex(c1.real - c2.real, c1.imag - c2.imag);
}
complex operator * (const complex &c1, const complex &c2)
{
return complex(c1.real * c2.real - c1.imag * c2.imag, c1.real * c2.real + c1.imag * c2.imag);
}
complex operator / (const complex &c1, const complex &c2)
{
return complex( (c1.real * c2.real + c1.imag * c2. imag) / (c2.real * c2.real + c2.imag * c2.imag),
(c1.imag * c2.real - c1.real * c2.imag) / (c2.real * c2.real + c2.imag * c2.imag) );
}
void print(const complex &c)
{
if(c.imag < 0)
cout<<c.real<<c.imag<<'i'<<endl;
else
cout<<c.real<<'+'<<c.imag<<'i'<<endl;
}
int main()
{
complex c1(2.0, 3.5), c2(6.7, 9.8), c3;
c3 = c1 + c2;
cout<<"c1 + c2 = ";
print(c3); //友元函数不是成员函数,只能采用普通函数调用方式,不能通过类的对象调用
c3 = c1 - c2;
cout<<"c1 - c2 = ";
print(c3);
c3 = c1 * c2;
cout<<"c1 * c2 = ";
print(c3);
c3 = c1 / c2;
cout<<"c1 / c2 = ";
print(c3);
return 0;
}
二、程序运行结果
从运行结果上我们就可以看出来,无论是通过成员函数方式还是采用友元函数方式,其实现的功能都是一样的,都是重载运算符,扩充其功能,使之能够应用于用户定义类型的计算中。
三、两种重载方式(成员函数方式与友元函数方式)的比较
一般说来,单目运算符最好被重载为成员;对双目运算符最好被重载为友元函数,双目运算符重载为友元函数比重载为成员函数更方便此,但是,有的双目运算符还是重载为成员函数为好,例如,赋值运算符。因为,它如果被重载为友元函数,将会出现与赋值语义不一致的地方。相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解