简体   繁体   English

如何“双重超载”操作员?

[英]How can I “double overload” an operator?

If I have a database-like class, and I want to do something like this: 如果我有一个类似数据库的类,我想做这样的事情:

object[1] == otherObject; object [1] == otherObject;

How do I "double overload" operator[] and operator==? 如何“双重超载”运算符[]和运算符==?

object[1] returns an object of someType . object[1]返回someType的对象。 You need to overload operator== on that someType . 你需要在someType上重载operator==

What you describe is simply two separate operator overloads. 您描述的只是两个独立的运算符重载。 Just make sure that the parameter to operator == matches the return type of operator [] . 只需确保operator ==的参数与operator []的返回类型匹配。

I was really confused at first by the wording of the question, but now I think I understand. 我最初对这个问题的措辞感到困惑,但现在我觉得我明白了。 You can't do this directly; 你不能直接这样做; but you can achieve the same effect. 但你可以达到同样的效果。 The idea is to make operator[] return a proxy object . 我们的想法是让operator[]返回一个代理对象 For the proxy object, you can provide operator== with custom behavior for comparison. 对于代理对象,您可以为operator==提供自定义行为以进行比较。 I've seen std::map's operator[] implemented this way on some older, more obscure standard library implementations. 我已经看到std :: map的operator []在一些较旧的,更模糊的标准库实现上实现了这种方式。

Example (let's say object[1] normally returns Foo&): 示例(假设object [1]通常返回Foo&):

class SomeProxy
{
private:
    Foo* f;
public:
    explicit SomeProxy(Foo& i_f): f(&i_f) {}
    operator Foo&() const {return *f;}
};

SomeProxy Database::operator[](unsigned int n)
{
     return SomeProxy(some_array + n);
}

bool operator==(const SomeProxy& lhs, const Foo& rhs)
{
    // provide custom behavior
}


// also provide custom behavior for these:
bool operator==(const SomeProxy& lhs, const SomeProxy& rhs);
bool operator==(const Foo& lhs, const SomeProxy& rhs);

Note: it seems kind of odd that you want to overload the meaning of a comparison operator in such a case (proxy objects are often used for custom assignment behavior with associative structures or for fancy metatemplate programming) but for comparison they should typically provide the same meaning as working with Foo directly, eg Nevertheless, here's the way to do it: a way to get custom behavior through operator== that is only applicable when using an overloaded operator[] in the same expression. 注意:在这种情况下,您希望重载比较运算符的含义似乎有点奇怪(代理对象通常用于具有关联结构的自定义赋值行为或用于花式metatemplate编程)但为了进行比较,它们通常应提供相同的意思是直接使用Foo ,例如,然而,这是实现它的方法:通过operator==获取自定义行为的方法仅适用于在同一表达式中使用重载operator[]时。

If you want to make this thorough, then make the constructors of SomeProxy private and make Database a friend. 如果你想彻底完成这个,那么让SomeProxy的构造函数成为私有的,并使数据库成为朋友。 This way, only the Database: can create those objects (through operator[] in this case) and the client won't be able to copy it (which he shouldn't), only get a Foo object/reference out of it or use the proxy returned in an expression. 这样,只有Database:可以创建那些对象(在这种情况下通过operator []),客户端将无法复制它(他不应该),只能从中获取Foo对象/引用或者使用表达式中返回的代理。

Don't think of operator overloading as anything special, they are just like normal function overloading. 不要认为运算符重载是特殊的,它们就像正常的函数重载一样。 Two functions having the same name can be overloaded given that they have different signatures. 具有相同名称的两个函数可以重载,因为它们具有不同的签名。 (You can't overload 2 functions only by the return type by the way. That is not just for operator overloading, it is for all overloading of functions in general.) (顺便说一句,你不能只通过返回类型重载2个函数。这不仅仅是对于运算符重载,它通常用于所有函数的重载。)

So simply like this: 所以简单地这样:

class B {};
class C {};
class A
{
  bool operator==(const B& b)
  {
      //Put logic here
      return true;
  }

  bool operator==(const C& c)
  {
      //Put logic here
      return true;
  }

};

The above code will allow you to compare an object of type A with an object of type B . 上面的代码将允许您将类型A的对象与类型B的对象进行比较。 It will also allow you to compare an object of type A with an object of type C . 它还允许您将类型A的对象与类型C的对象进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM