我怎样才能“双重超载”?操作员?

发布于 2024-09-08 05:37:12 字数 96 浏览 4 评论 0原文

如果我有一个类似数据库的类,并且我想做这样的事情:

object[1] == otherObject;

如何“双重重载”运算符[]和运算符==?

If I have a database-like class, and I want to do something like this:

object[1] == otherObject;

How do I "double overload" operator[] and operator==?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

生死何惧 2024-09-15 05:37:12

object[1] 返回 someType 的对象。您需要在该 someType 上重载 operator==

object[1] returns an object of someType. You need to overload operator== on that someType.

半透明的墙 2024-09-15 05:37:12

您所描述的只是两个单独的运算符重载。只需确保 operator == 的参数与 operator [] 的返回类型匹配即可。

What you describe is simply two separate operator overloads. Just make sure that the parameter to operator == matches the return type of operator [].

夜清冷一曲。 2024-09-15 05:37:12

起初我对问题的措辞感到非常困惑,但现在我想我明白了。你不能直接这样做;但你可以达到同样的效果。这个想法是让operator[]返回一个代理对象。对于代理对象,您可以为 operator== 提供自定义行为以进行比较。我见过 std::map 的 operator[] 在一些较旧的、更晦涩的标准库实现上以这种方式实现。

示例(假设 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);

注意:在这种情况下您想要重载比较运算符的含义似乎有点奇怪(代理对象通常用于具有关联结构的自定义赋值行为或对于花哨的元模板编程),但为了进行比较,它们通常应该提供与直接使用 Foo 相同的含义,例如尽管如此,这里是实现它的方法:一种通过 operator= 获取自定义行为的方法= 仅当在同一表达式中使用重载的operator[] 时才适用。

如果你想彻底做到这一点,那么请将 SomeProxy 的构造函数设为私有,并将 Database 设为友元。这样,只有 Database: 可以创建这些对象(在本例中通过运算符[]),并且客户端将无法复制它(他不应该这样做),只能从中获取 Foo 对象/引用或使用表达式中返回的代理。

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. For the proxy object, you can provide operator== with custom behavior for comparison. I've seen std::map's operator[] implemented this way on some older, more obscure standard library implementations.

Example (let's say object[1] normally returns 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, e.g. 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.

If you want to make this thorough, then make the constructors of SomeProxy private and make Database a friend. 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.

千寻… 2024-09-15 05:37:12

不要认为运算符重载有什么特别的,它们就像普通的函数重载一样。两个具有相同名称的函数可以重载,因为它们具有不同的签名。 (顺便说一下,你不能仅通过返回类型来重载 2 个函数。这不仅适用于运算符重载,也适用于一般函数的所有重载。)

就像这样简单:

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;
  }

};

上面的代码将允许您比较A 类型的对象与 B 类型的对象。它还允许您将 A 类型的对象与 C 类型的对象进行比较。

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.)

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. It will also allow you to compare an object of type A with an object of type C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文