运算符重载中的隐式转换问题

问题描述:


谭浩强c++书中运算符重载隐式转换关于以下两句话的问题
friend Complex operator+(Complex&, Complex&);
friend Complex operator+(const Complex&,const Complex &);

知识点:


1.隐式转换不会被用于非const的引用参数
2.二义性问题
3.为什么用友元函数


详情:

#include<iostream>
#include<iomanip>
using namespace std;
class Complex
{
public:
	Complex(){ real = 0; imag = 0; }
	Complex(float r){ real = r; imag = 0; }//转换构造函数 
	Complex(float r, float i) :real(r), imag(i){}
	friend Complex operator+(Complex&, Complex&);//没有二义性  
	operator double() const { return real; } //转换操作符,必须为成员函数,形参列表为空,,一般不应该改变被转换的对象,通常定义为
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值