操作符重载(朋友和成员函数) [英] operator overloading(friend and member function)

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

问题描述

使用 friend 关键字和作为类中的成员函数的运算符重载有什么区别?



解决方案

在任何一元运算符重载的情况下,

Jacob是正确的...一个朋友在一个类中声明的函数可以访问该类,但它不在类中,所有人都可以访问它。



对于不是类成员的运算符重载(也称为自由函数,它可能是朋友, ),参数与操作数相同。对于一个类的成员,第一个操作数是隐式参数,它变成 this



隐式参数与自由函数的第一个参数在以下几个方面不同:




  • 它的类型是引用到类,而自由函数可以为其第一个参数声明任何类型。

  • 它不参与隐式类型转换。 (它不会是由转换构造函数初始化的)。

  • 它参与虚拟覆盖解析。 (A virtual 重载将由第一个操作数的动态类型选择,这是免费的功能不可能没有额外的代码。)



一元,二元或n元的情况是一样的(在 operator()的情况下)

>, = ,前缀 ++ )应该作为成员函数实现,并且应该完全实现所有重载。 Postfix ++ 是一个二等公民;它被实现为 Obj ret = * this; ++这个; return ret; 。请注意,这有时扩展到复制构造函数,它可能包含 * this = initializer



规则自由的通勤者:只有交换运算符(例如 / )应该是自由功能;所有其他运营商(例如一元化)应该是成员。交换操作符固有地创建对象的副本;它们被实现为 Obj ret = lhs; ret @ = rhs; return ret; 其中 @ 是交换运算符,而 lhs rhs 分别是左侧和右侧的参数。



C ++友谊的黄金规则:避免友谊。 friend 污染设计的语义。 重载推论:如果您遵循上述规则,则重载过程很简单,那么朋友是无害的。 friend 模板超载定义允许它们被放置在类{大括号内。



注意,有些操作符不能是自由函数: = - > [] (),因为标准在第13.5节中具体说明。我认为这一切...我认为一元的& * 也是,但我显然错了。 总是作为成员重载,但只有经过仔细思考!


What is the difference between operator overloading using the friend keyword and as a member function inside a class?

Also, what is the difference in the case of any unary operator overloading (i.e. as a friend vs. as a member function)?

解决方案

Jacob is correct… a friend function declared within a class has access to that class, but it's not inside the class at all, and everyone else has access to it.

For an operator overload which is not a member of the class (also called a free function, it may be a friend, or maybe not), the arguments are the same as the operands. For one which is a member of a class, the first operand is the "implicit argument" which becomes this.

The implicit argument is different from the first argument to a free function in a few ways:

  • Its type is reference-to-class, whereas the free function can declare any type for its first argument.
  • It does not participate in implicit type conversion. (It will not be a temporary initialized by a conversion constructor.)
  • It does participate in virtual override resolution. (A virtual overload will be chosen by the dynamic type of the first operand, which is not possible with free functions without extra code.)

The situation is the same for unary, binary, or n-ary (in the case of operator()).

Members privilege of mutation: Operators which change the first operand (eg +=, =, prefix ++) should be implemented as member functions, and should exclusively implement the guts of all overloads. Postfix ++ is a second-class citizen; it is implemented as Obj ret = *this; ++ this; return ret;. Note that this sometimes extends to copy-constructors, which may contain *this = initializer.

Rule of freedom for commuters: Only commutative operators (eg /) should be free functions; all other operators (eg unary anything) should be members. Commutative operators inherently make a copy of the object; they are implemented as Obj ret = lhs; ret @= rhs; return ret; where @ is the commutative operator and lhs and rhs are left-hand side and right-hand side arguments, respectively.

Golden rule of C++ friendship: Avoid friendship. friend pollutes the semantics of a design. Overloading corollary: Overloading is simple if you follow the above rules, then friend is harmless. friending boilerplate overload definitions allows them to be placed inside the class { braces.

Note that some operators cannot be free functions: =, ->, [], and (), because the standard specifically says so in section 13.5. I think that's all… I thought unary & and * were too, but I was apparently wrong. They should always be overloaded as members, though, and only after careful thought!

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

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