同一个运算符的C ++多个运算符重载 [英] C++ multiple operator overloads for the same operator

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

问题描述

我知道通过生成代码并查看其是否可以编译,我可以轻松地为自己回答这个问题.但是由于找不到类似的问题,我认为值得分享知识. 假设我正在为MyClass重载+运算符.我可以多次过载吗?不同类型的过载不同.像这样:

I know I can answer this question easily for myself by generatin the code and see if it compiles. But since I couldn't find a similar question, I thought it's knowledge worth sharing. Say I am overloading the + operator for MyClass. Can I overload it multiple times. Different overload for different types. Like this:

class MyClass{
...
inline const MyClass operator+(const MyClass &addend) const {
    cout<<"Adding MyClass+MyClass"<<endl;
    ...//Code for adding MyClass with MyClass
}
inline const MyClass operator+(const int &addend) const {
    cout<<"Adding MyClass+int"<<endl;
    ...//Code for adding MyClass with int
}
...
};
int main(){
    MyClass c1;
    MyClass c2;
    MyClass c3 = c1 + c2; 
    MyClass c4 = c1 + 5;
}
/*Output should be:
  Adding MyClass+MyClass
  Adding MyClass+in*/

我要这样做的原因是,我正在构建一个我想尽可能优化的类.在这里,性能是我最大的担忧.因此,在操作员+重载函数内部强制转换并使用开关盒是不可行的.如果您会注意到,我将这两个重载都内联了.让我们先假设编译器确实内联了我的重载,然后在编译时预先确定了将要运行的代码,然后将调用保存到函数中(通过内联)+复杂的切换情况(实际上, +运算符有5+个重载),但仍然能够使用基本算术运算符轻松编写可读代码. 那么,我会得到想要的行为吗?

The reason I want to do this is that I am building a class that I want to be as optimized as possible. Performance is the biggest concern for me here. So casting and using switch case inside the operator + overloaded function is not an option. I f you'll notice, I made both the overloads inline. Let's assume for a second that the compiler indeed inlines my overloads, then it is predetermined at compile time which code will run, and I save the call to a function (by inlining) + a complicated switch case scenario (in reality, there will be 5+ overloads for + operator), but am still able to write easily read code using basic arithmetic operators. So, will I get the desired behavior?

推荐答案

实现operator+()的规范形式是基于operator+=()的免费功能,当您拥有+时,您的用户会期望它. +=更改其左手参数,因此应为成员. +对称地对待其参数,因此应该是一个自由函数.

The canonical form of implementing operator+() is a free function based on operator+=(), which your users will expect when you have +. += changes its left-hand argument and should thus be a member. The + treats its arguments symmetrically, and should thus be a free function.

类似的事情应该做:

//Beware, brain-compiled code ahead!
class MyClass {
public:
    MyClass& operator+=(const MyClass &rhs) const
    {
      // code for adding MyClass to MyClass
      return *this;
    }
    MyClass& operator+=(int rhs) const
    {
      // code for adding int to MyClass
      return *this;
    }
};


inline MyClass operator+(MyClass lhs, const MyClass& rhs) {
  lhs += rhs;
  return lhs;
}
inline MyClass operator+(MyClass lhs, int rhs) {
  lhs += rhs;
  return lhs;
}
// maybe you need this one, too
inline MyClass operator+(int lhs, const MyClass& rhs) {
  return rhs + lhs; // addition should be commutative
}

(请注意,用其类的定义定义的成员函数是隐式的inline.还请注意,在MyClass中,不需要前缀MyClass::甚至是错误的.)

(Note that member functions defined with their class' definition are implicitly inline. Also note, that within MyClass, the prefix MyClass:: is either not needed or even wrong.)

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

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