操作符重载 [英] Operator overloading

查看:116
本文介绍了操作符重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么重载operator =强制为成员函数($ 13.5.3),而不是复合赋值运算符。 operator + =($ 13.5.2)? c> $

作为成员,如果用户没有定义一个,它总是由编译器提供。我相信这只是为了简单和避免意想不到的歧义,它要求 operator = 不能被定义为自由函数。



转换运算符处理从用户定义类型到内置类型的转换。


Why is overloaded operator= mandated to be a member function ($13.5.3), but not a compound assignment operator e.g. operator+= ($13.5.2)? Am I overlooking something here?

解决方案

A copy assignment operator=, as a member, is always provided by the compiler if the user doesn't define one. I believe that it was only for simplicity and to avoid unexpected ambiguities that it was made a requirement that operator= can't be defined as a free function.

Conversion operators take care of the case when you want to assign from a user-defined type to a built-in type.

这篇关于操作符重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆