5.2.2 友元运算符重载函数

运算符重载函数一般采用两种形式定义:
一是定义为它将要操作的类的成员函数(简称运算符重载函数);
二是定义为类的友元函数(简称为友元运算符重载函数)。

1.定义友元运算符重载函数的语法如下:

    在类的内部:  friend 函数类型 operator运算符(形参表)

函数体
} 在类的内部声明,在类外定义:
class X{
...
friend 函数类型 operator运算符(形参表);
};
函数类型 X::operator运算符(形参表)

函数体

其中,X是友元函数的运算符重载函数所在类的类名;函数类型指定了友元运算符函数的返回值类型;operator是定义运算符重载函数的关键字;运算符即是要重载的运算符名称,必须是C++中可重载的运算符;形参表中给出重载运算符所需要的参数和类型;关键字friend表明这是一个友元运算符重载函数。由于友元运算符重载函数不是该类的成员函数,所以在类外定义时不必缀上类名。

注意:若友元运算符重载函数重载的是双目运算符,则参数表中有两个操作数;若友元运算符重载函数重载的是单目运算符,则参数表中只有一个操作数。

例5.2 用友元运算符重载函数进行复数的运算。

加法:(a+bi)+(c+di)=(a+c)+(b+d)i
减法:(a-bi)+(c-di)=(a-c)+(b-d)i
乘法:(a-bi)*(c-di)=(ac-bd)+(ad+bc)i
除法:(a-bi)/(c-di)=(a+bi)*(c-di)/(c*c+d*d)

#include<iostream>
using namespace std;
class Complex{
public:
Complex(){};
Complex(double r,double i)
{
real = r;
imag = i;
}
void print();
friend Complex operator+(Complex &co1,Complex &co2); //声明运算符+的重载函数
friend Complex operator-(Complex &co1,Complex &co2); //声明运算符-的重载函数
friend Complex operator*(Complex &co1,Complex &co2); //声明运算符*的重载函数
friend Complex operator/(Complex &co1,Complex &co2); //声明运算符/的重载函数
private:
double real;//复数的实部
double imag;//复数的虚部
};
Complex operator+(Complex &co1,Complex &co2) //定义运算符+的重载函数
{
Complex temp;
temp.real = co1.real+co2.real;
temp.imag = co1.imag+co2.imag;
return temp; //return Complex(co1.real+co2.real,co1.imag+co2.imag)
}
Complex operator-(Complex &co1,Complex &co2) //定义运算符-的重载函数
{
Complex temp;
temp.real = co1.real-co2.real;
temp.imag = co1.imag-co2.imag;
return temp; //return Complex(co1.real-co2.real,co1.imag-co2.imag)
}
Complex operator*(Complex &co1,Complex &co2) //定义运算符*的重载函数
{
Complex temp;
temp.real = co1.real*co2.real-co1.imag*co2.imag;
temp.imag = co1.real*co2.imag+co1.imag*co2.real;
return temp;
}
Complex operator/(Complex &co1,Complex &co2) //定义运算符/的重载函数
{
Complex temp;
double t;
t = /(co2.real*co2.real+co2.imag*co2.imag);
temp.real = (co1.real*co2.real+co1.imag*co2.imag)*t;
temp.imag = (co2.real*co1.imag-co1.real*co2.imag)*t;
return temp;
}
void Complex::print()
{
cout<<real;
cout<<"+"<<imag<<'i'<<endl;
}
int main()
{
Complex A1(2.3,4.6),A2(3.6,2.8),A3,A4,A5,A6;
A3 = A1+A2; //A3 = operaotr+(A1,A2)
A4 = A1-A2; //A3 = operaotr-(A1,A2)
A5 = A1*A2; //A3 = operaotr*(A1,A2)
A6 = A1/A2; //A3 = operaotr/(A1,A2)
A1.print();
A2.print();
A3.print();
A4.print();
A5.print();
A6.print();
return ;
}

说明:

(1) 一般而言,如果在类X中采用友元函数重载双目运算符@,而aa和bb是类的两个子类对象,则有以下两种函数调用方法是等价的。

 aa@bb;                //隐式调用
operator@(aa,bb); //显示调用 例如: A3 = A1+A2; ============ A3 = operaotr+(A1,A2)
A4 = A1-A2; ============ A4 = operaotr-(A1,A2)
A5 = A1*A2; ============ A5 = operaotr*(A1,A2)
A6 = A1/A2; ============ A6 = operaotr/(A1,A2)

(2)有时,在函数返回时候,可以直接在类的构造函数来生成一个临时对象,而不对该对象进行命名,例如将上述重载运算符+友元运算符重载函数

 Complex operator+(Complex &co1,Complex &co2)
{
Complex temp;
temp.real = co1.real+co2.real;
temp.imag = co1.imag+co2.imag;
return temp;
}
改为:
Complex operator+(Complex &co1,Complex &co2)
{
return Complex(co1.real-co2.real,co1.imag-co2.imag) //创建了一个临时的无名对象
}

(3)有的C++系统(如Visual C++6.0)没有完全实现C++标准,它所提供的不带后缀.h文件不支持友元运算符重载函数,在Visual C++6.0中编译错误,这是可以采用带后缀的“.h”头文件。

     即将 #include<iostream>
using namespace std 改为 #include<iostream.h>

例5.2 用友元运算符重载函数进行复数的运算。
加法:(a+bi)+(c+di)=(a+c)+(b+d)i
减法:(a-bi)+(c-di)=(a-c)+(b-d)i
乘法:(a-bi)*(c-di)=(ac-bd)+(ad+bc)i
除法:(a-bi)/(c-di)=(a+bi)*(c-di)/(c*c+d*d)

#include<iostream>
using namespace std;
class Complex{
public:
Complex(){};
Complex(double r,double i)
{
real = r;
imag = i;
}
void print();
friend Complex operator+(Complex &co1,Complex &co2); //声明运算符+的重载函数
friend Complex operator-(Complex &co1,Complex &co2); //声明运算符-的重载函数
friend Complex operator*(Complex &co1,Complex &co2); //声明运算符*的重载函数
friend Complex operator/(Complex &co1,Complex &co2); //声明运算符/的重载函数
private:
double real;//复数的实部
double imag;//复数的虚部
};
Complex operator+(Complex &co1,Complex &co2) //定义运算符+的重载函数
{
return Complex(co1.real+co2.real,co1.imag+co2.imag);
}
Complex operator-(Complex &co1,Complex &co2) //定义运算符-的重载函数
{
return Complex(co1.real-co2.real,co1.imag-co2.imag);
}
Complex operator*(Complex &co1,Complex &co2) //定义运算符*的重载函数
{
return Complex(co1.real*co2.real-co1.imag*co2.imag,co1.real*co2.imag+co1.imag*co2.real);
}
Complex operator/(Complex &co1,Complex &co2) //定义运算符/的重载函数
{
double t;
t = /(co2.real*co2.real+co2.imag*co2.imag);
return Complex((co1.real*co2.real+co1.imag*co2.imag)*t,(co2.real*co1.imag-co1.real*co2.imag)*t);
}
void Complex::print()
{
cout<<real;
cout<<"+"<<imag<<'i'<<endl;
}
int main()
{
Complex A1(2.3,4.6),A2(3.6,2.8),A3,A4,A5,A6;
A3 = A1+A2; //A3 = operaotr+(A1,A2)
A4 = A1-A2; //A3 = operaotr-(A1,A2)
A5 = A1*A2; //A3 = operaotr*(A1,A2)
A6 = A1/A2; //A3 = operaotr/(A1,A2)
A1.print();
A2.print();
A3.print();
A4.print();
A5.print();
A6.print();
return ;
}

3. (友元运算符重载函数中)单目运算符重载

单目运算符只有一个操作数,如-a,&b,!c,++p等。重载单目运算符的方法与重载双目运算符的方法类似的。

注意:有友元函数重载单目运算符时,需要一个显示的操作数。

//例 5.3 用友元函数重载单目运算符"-"

#include<iostream>
using namespace std;
class Coord{
public:
Coord(int x=,int y=)
{
i = x;
j = y;
}
void print()
{
cout<<"i="<<i<<",";
cout<<"j="<<j<<endl;
}
friend Coord operator-(Coord a); //声明友元单目运算符-(负号)重载函数
private:
int i;
int j;
};
Coord operator-(Coord obj) //定义友元单目运算符-(负号)重载函数
{
obj.i = -obj.i;
obj.j = -obj.j;
return obj;
}
int main()
{
Coord ob1(,),ob2;
ob1.print();
ob2 = -ob1;
ob2.print();
return ;
}

//例5.4 用友元函数重载单目运算符"++"

#include<iostream>
using namespace std;
class Coord{
public:
Coord(int x=,int y=)
{
i = x;
j = y;
}
friend Coord operator++(Coord &c) //定义友元运算符++"自加"的重载函数
{
c.i++;
c.j++;
return c;
}
void print()
{
cout<<"i="<<i<<","<<"j="<<j<<endl;
}
private:
int i;
int j;
};
int main()
{
Coord ob1(,),ob2;
ob1.print(); ++ob1; //隐式调用友元运算符重载函数
ob1.print(); operator++(ob1); //显式调用友元运算符重载函数
ob1.print(); ob2=++ob1; //隐式调用友元运算符重载函数
ob1.print(); ob2=operator++(ob1); //显式调用友元运算符重载函数
ob2.print(); return ;
}

注意:
使用友元运算符重载单目运算符"++"时,形参是对象的引用,是通过传址的方法传递参数的,函数形参op.x和op.y的改变将引起实参op.x和op.y的变化。

但是,如果形参是对象的话,是通过值传递的方法传递参数的,函数体内对形参op的所有修改都无法传递到函数体外。也就是说,实际上形参值的改变不能引起实参值的改变。

一般而言,如果在类X中采用友元函数重载单目运算符@,而aa是类X的对象,则以下两种函数调用方法是等价的:

@aa;                 //隐式调用
operator@(aa); //显示调用

说明:
(1)运算符重载函数operator@可以是任何类型,甚至可以是void类型,但通常返回类型与所操作的类的类型相同,这样可以使重载运算符用在复杂的表达式中。

(2)有的运算符不能定义为友元运算符重载函数,如赋值运算符"="、下标运算符[]、函数调用运算符"()"等。

C++:运算符重载函数之友元运算符重载的更多相关文章

  1. C++:成员运算符重载函数和友元运算符重载函数的比较

    5.2.4 成员运算符重载函数和友元运算符重载函数的比较 (1)对双目运算符而言,成员运算符重载函数参数表中含有一个参数,而友元运算符重载函数参数表中有两个参数:对于单目运算符而言,成员运算符重载函数 ...

  2. Win32 API编程:WinMain无法重载函数或_tWinMain无法重载

    #include "windows.h" #include "tchar.h" int APIENTRY _tWinMain( HINSTANCE hInsta ...

  3. C++:运算符重载函数之成员运算符重载函数

    5.2.3 成员运算符重载函数 在C++中可以把运算符重载函数定义为某个类的成员函数,称之为成员运算符重载函数. 1. 定义成员运算符重载函数的语法形式 (1)在类的内部,定义成员运算符重载函数的格式 ...

  4. C++学习之路—运算符重载(二)运算符重载作为类的成员函数和友元函数

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 对运算符重载的函数有两种处理方式:(1)把运算符 ...

  5. C++:友元运算符重载函数

    运算符重载函数:实现对象之间进行算数运算,(实际上是对象的属性之间做运算),包括+(加号).-(减号).*./.=.++.--.-(负号).+(正号) 运算符重载函数分为:普通友元运算符重载函数.成员 ...

  6. C++:运算符重载函数

    5.运算符重载 5.1 在类外定义的运算符重载函数 C++为运算符重载提供了一种方法,即在运行运算符重载时,必须定义一个运算符重载函数,其名字为operator,后随一个要重载的运算符.例如,要重载& ...

  7. C++ 基础语法 快速复习笔记(3)---重载函数,多态,虚函数

    1.重载运算符和重载函数: C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载. 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它 ...

  8. MySQL(六) —— 运算符和函数

    1. 字符函数   函数名称 描述 CONCAT()         字符连接                             CONCAT_WS()          使用指定的分隔符进行字 ...

  9. 你好,C++(28)用空间换时间 5.2 内联函数 5.3 重载函数

    5.2  内联函数 通过5.1节的学习我们知道,系统为了实现函数调用会做很多额外的幕后工作:保存现场.对参数进行赋值.恢复现场等等.如果函数在程序内被多次调用,且其本身比较短小,可以很快执行完毕,那么 ...

随机推荐

  1. SQL2012 附加数据库提示5120错误解决方法

    在win8.1 x64系统上使用sql2012进行附加数据库(包括在x86系统正在使用的数据库文件,直接拷贝附加在X64系统中)时,提示无法打开文件,5120错误. 这个错误是因为没有操作权限,所以附 ...

  2. 导入导出Android手机文件

    1.获得root权限:adb root: 如提示adbd cannot run as root in production builds,参见我的另一篇文章:http://www.cnblogs.co ...

  3. QQ群里收集的外企iOS开发的笔试题

    一组外企iOS开发的笔试题,您能回答出来吗?从群里收集来的. 1 why can't NSArray contain NSInteger Instance? with which extra step ...

  4. c/c++常用代码--清空目录

    #pragma once #include <io.h>#include <stdio.h>#include <string>#include <direct ...

  5. 如何自定义Liferay 7 portal的Log in登录界面

    前提: 1. Liferay portal 7 2. Liferay IDE 3.0.1 Liferay现有的工具中提供了很多修改portal的模板,以满足开发者的各种自定义需求. 修改的原理是利用M ...

  6. windows最基本命令行

    7:计算机运行命令全集 winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构 wupdmgr--------windows更新程序 win ...

  7. execvp使用实例

    问题描述:           本程序实现模拟shell功能,用户输入命令,返回相应的结果 问题解决: 注:        以上指出了execvp函数的使用,使用时第一个参数是文件名,第二个参数是一个 ...

  8. mvc 分页js

    <script type="text/javascript">     var configA = {         options: {             m ...

  9. maven3常用命令、java项目搭建、web项目搭建详细图解

    http://blog.csdn.net/edward0830ly/article/details/8748986 ------------------------------maven3常用命令-- ...

  10. C#的cs文件中Response.Clear();Response.ClearHeaders()的作用

    在学习一个CS文件,如下:public partial class GetPic : System.Web.UI.Page{    protected void Page_Load(object se ...