当前位置: 首页 > article >正文

c语言运算符重载格式,运算符重载函数的两种形式

(1)重载为类的成员函数

格式如下:operator()

class complex

{

public:

complex()

{real=imag=0;}

complex(double r,double

i)

{

real=r;

imag=i;

}

complex operator +(const

complex &c);

complex operator -(const

complex &c);

complex operator *(const

complex &c);

complex operator /(const

complex &c);

private:

double real,imag;

};

inline complex complex::operator+(const complex &c)

{

return

complex(real+c.real,imag+c.imag);

}

inline complex complex::operator-(const complex

&c)

{

return

complex(real-c.real,imag-c.imag);

}

inline complex complex::operator*(const complex

&c)

{

return

complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);

}

inline complex complex::operator/(const

complex &c)

{

return

complex((real*c.real+imag*c.imag)/(c.real*c.real+c.imag*c.imag),(imag*c.real-real*c.imag)/(c.real*c.real+c.imag*c.imag));

}

有complex c1,c2,c3;

c1+c2表示为c1.operator+(c2)其中operator+()是运算符+的重载函数。

由此得到当运算符重载为成员函数时,双目运算符仅有一个参数。单目运算符重载为成员函数时,不能再显示的说明参数。重载为成员函数时总是隐含一个参数,该参数是this指针。this指针式指向调用该成员函数对象的指针。

(2)重载为类的友元函数

重载为友元函数时,将没有隐含的参数this指针。对双目运算符有两个参数,单目运算符有一个参数

=,(),[],->不宜重载为友元函数。

格式如下:friend operator ()

class complex

{

public:

complex()

{real=imag=0;}

complex(double r,double

i)

{

real=r;

imag=i;

}

friend complex operator +(const

complex &c);

friend complex operator -(const

complex &c);

friend complex operator *(const

complex &c);

friend complex operator /(const

complex &c);

friend void print(const complex

&c);

private:

double real,imag;

};

inline complex complex::operator+(const complex

&c)

{

return

complex(real+c.real,imag+c.imag);

}

inline complex complex::operator-(const complex

&c)

{

return

complex(real-c.real,imag-c.imag);

}

inline complex complex::operator*(const complex

&c)

{

return

complex(real*c.real-imag*c.imag,real*c.imag+imag*c.real);

}

inline complex complex::operator/(const

complex &c)

{

return

complex((real*c.real+imag*c.imag)/(c.real*c.real+c.imag*c.imag),(imag*c.real-real*c.imag)/(c.real*c.real+c.imag*c.imag));

} void print (const complex &c)

{

if (c.imag<0)

cout<

else

cout<

}

此时的c1+c2表示为operator +(c1,c2)

(3)通常单目运算符最好被重载为成员函数,双目运算符最好被重载为友元函数,有些情况下双目运算符不便于重载为友元函数

例如:c+4.45 c是complex对象

+重载为成员函数时有c.operator+(4.45)通过调用构造函数得到c.operator+(complex(4.45))

+重载为友元函数时有operator+(c,4.45)

通过调用构造函数得到operator+(c,complex(4.45))

若是4.45+c

成员函数对应为4.45.operator+(c)//该种方法不对

友元函数对应为operator+(complex(4.45),c)

例如

http://www.lryc.cn/news/2420819.html

相关文章:

  • Java中随机数函数Random()详解+案例
  • 获取字符串首字母拼音
  • Android快速入门_总结
  • 十个让你获得高质量外链资源的方法
  • 基于Qualcomm Robotics RB5边缘侧AI应用—人群热力图
  • joomla本地安装全过程
  • [风潮]三颗猫饼干
  • VM虚拟机和Centos镜像官网的下载、安装和激活(超详细教程)
  • ajax详解以及各种封装
  • GIS中的空间参考
  • read失败报错1 Operation not permitted
  • PHP汉字转拼音(有声调、无声调、首字母、首字母大写)
  • 软件工程之项目管理
  • 基于MDK编程STM32程序无法使用,硬件仿真在汇编窗口看到停留在“0x0800XXXX BEAB BKPT 0xAB //进入调试模式”...
  • 高频词汇快背
  • w7下如何安装linux双系统,Win7下安装UBuntu双系统详细教程
  • 行为识别系列:win11系统AVA2.1数据集制作、训练、测试、本地视频验证(完整已跑通)
  • 你想要的宏基因组-微生物组知识全在这(2023.5)
  • 我快被食人花吃掉了。
  • 2018蓝桥杯C/C++ A组C组题目汇总
  • 2023年“移动开发”技术再洗牌,IOS砍一半,Android砍三分之一
  • 心情不好时看看这些
  • 如何结合Node和Puppeteer做网络爬虫
  • 广告中的CPM、CPC、CPA解释
  • 一份较为详细的深度学习资料汇总
  • 如何观看高清Youtube视频和高清视频的转帖方法
  • 最新QQ空间免费导航代码
  • 公安大数据平台建设项目
  • 关键词优化完整 “操作 “指南
  • 浅谈快表