为什么在类之外定义operator +或+ =,以及如何正确地做? [英] Why define operator + or += outside a class, and how to do it properly?

查看:636
本文介绍了为什么在类之外定义operator +或+ =,以及如何正确地做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对

之间的区别有点困惑。

 类型运算符+(const Type& type) 
Type& operator + =(const Type& type);

  friend类型运算符+(const Type& type1,const Type& type2); 
friend Type& operator + =(const Type& type1,const Type& type2);

哪种方式是首选,它们是什么样子和什么时候应该使用?

解决方案

第一种形式的运算符是你在类 Type / p>

第二种形式的运算符是在类 Type 中定义为独立函数。



这是一个非常好的主意来定义自由的函数,因为那些的操作数可以参与隐式转换。

>

示例



假设这个类:

  class Type {
public:
Type(int foo){}

//添加const限定符作为更新:see end of answer
类型operator +(const Type& type)const {return * this; }
};

然后您可以写:

 键入a = Type(1)+ Type(2); // OK 
类型b =类型(1)+ 2; //也OK:将int(2)转换为类型

但是你不能写: p>

 类型c = 1 +类型(2) // Do not NOT COMPILE 

具有 operator + 一个自由函数允许最后一种情况。



运算符的第二种形式错误的是,它通过直接调整其操作数的私有成员来执行添加(我假设,否则它不需要是朋友)。



为了看看如何实现,让我们请求一个大师的服务: http://www.gotw.ca/gotw /004.htm





更新: $ b

正如James McNellis在他的评论中所说的,给出的两种形式也有另一个区别:左侧在第一个版本中不是const限定的。由于 operator + 的操作数实际上不应该作为添加的一部分进行修改,因此对它们进行const限定是一个非常好的主意。

结论

$

b
$ b

处理运算符 + + = 的最佳方法是: / p>


  1. 定义运算符+ = T :: operator + =(const T&);

  2. 运算符+ 定义为 TT :: operator +(const T& )const; 在你的类中。

  3. 提供一个自由函数 T operator +(const T& const T&); 在类外,但在同一个命名空间内。此函数将调用成员 operator + 执行此操作。

省略步骤2,并且让自由函数直接调用 T :: operator + = ,但是作为个人偏好,我想保留类中的所有加法逻辑。


I am a bit confused about the differences between

Type  operator +  (const Type &type);
Type &operator += (const Type &type);

and

friend Type  operator +  (const Type &type1, const Type &type2);
friend Type &operator += (const Type &type1, const Type &type2);

which way is preferred, what do they look like and when should either be used?

解决方案

The first form of the operators is what you would define inside class Type.

The second form of the operators is what you would define as free-standing functions in the same namespace as class Type.

It's a very good idea to define free-standing functions because then the operands to those can take part in implicit conversions.

Example

Assume this class:

class Type {
    public:
    Type(int foo) { }

    // Added the const qualifier as an update: see end of answer
    Type operator + (const Type& type) const { return *this; }
};

You could then write:

Type a = Type(1) + Type(2); // OK
Type b = Type(1) + 2; // Also OK: conversion of int(2) to Type

But you could NOT write:

Type c = 1 + Type(2); // DOES NOT COMPILE

Having operator+ as a free function allows the last case as well.

What the second form of the operator does wrong though is that it performs the addition by directly tweaking the private members of its operands (I 'm assuming that, otherwise it would not need to be a friend). It should not be doing that: instead, the operators should also be defined inside the class and the free-standing functions should call them.

To see how that would turn out, let's ask for the services of a guru: http://www.gotw.ca/gotw/004.htm. Scroll at the very end to see how to implement the free-standing functions.

Update:

As James McNellis calls out in his comment, the two forms given also have another difference: the left-hand-side is not const-qualified in the first version. Since the operands of operator+ should really not be modified as part of the addition, it's a very very good idea to const-qualify them all the time. The class Type in my example now does this, where initially it did not.

Conclusion

The best way to deal with operators + and += is:

  1. Define operator+= as T& T::operator+=(const T&); inside your class. This is where the addition would be implemented.
  2. Define operator+ as T T::operator+(const T&) const; inside your class. This operator would be implemented in terms of the previous one.
  3. Provide a free function T operator+(const T&, const T&); outside the class, but inside the same namespace. This function would call the member operator+ to do the work.

You can omit step 2 and have the free function call T::operator+= directly, but as a matter of personal preference I 'd want to keep all of the addition logic inside the class.

这篇关于为什么在类之外定义operator +或+ =,以及如何正确地做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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