简体   繁体   English

如何在C ++中比较两种泛型类型?

[英]How do I compare two generic types in C++?

I need to determine whether an element is the same as the one I'm passing by reference. 我需要确定一个元素是否与我通过引用传递的元素相同。 In the function Belongs I need to compare equality between d is and an element of a stored in a dynamic list: 在函数Belongs我需要比较d is和动态列表中存储的元素之间的相等性:

struct Nodo{ Dominio dominio; Rando rango; Nodo* next; }; 
typedef Nodo* ptrNodo; 
ptrNodo pri; 

template<class Dominio, class Rango>
bool DicListas<Dominio,Rango>::Belongs(const Dominio &d)
{
  bool retorno = false;
  if(!EsVacia())
  {
    ptrNodo aux=pri;

    while(aux!=NULL)
    {
      if(aux->dominio==d)//-------> THIS CLASS DOESN'T KNOW HOW TO COMPARE THE TYPE DOMINIO.
      {
        retorno = aux->isDef;
      }
      aux = aux->sig;
    }
  }
  return retorno;
}

Whatever type argument you provide for the type parameter Dominio , you've to overload operator== for that type. 无论您为类型参数 Dominio提供什么类型参数, Dominio为该类型重载operator==

Suppose, you write this: 假设,你写这个:

DicListas<A,B>  obj;
obj.Belongs(A());

then you've to overload operator== for the type A as: 然后你要为类型A重载operator==

class A
{
 public:
    bool operator == (const A &a) const
    {
       //compare this and a.. and return true or false
    }
};

Also note that it should be public if it's a member function, and better make it const function as well, so that you can compare const objects of type A . 另请注意,如果它是成员函数,它应该是public的,并且最好使它成为const函数,以便您可以比较类型A const对象。

Instead of making it member function, you can make operator== a non-member function as well: 您可以使operator==成为非成员函数,而不是使其成为成员函数:

bool operator == (const A &left, const A & right)
{
     //compare left and right.. and return true or false
}

I would prefer the latter. 我更喜欢后者。

It reduces to defining an overload of operator== for the user-defined type: 它减少了为用户定义的类型定义operator==的重载:

bool operator==(const WhateverType &a, const WhateverType &b)
{
    return whatever;
}

or maybe as a member of WhateverType . 或者作为WhateverType的成员。

If you want to compare something about two, possibly distinct, types, you probably want to look at either Boost type traits or the versions of type traits that made it into TR1 and C++11 (if you're using a compiler that supports either TR1 or C++11). 如果你想比较约两 ,可能是不同的,类型的东西,你可能想看看在任升压型特质 ,如果你正在使用的编译器支持或类型的特征,使得它成为TR1的版本和C ++ 11( TR1或C ++ 11)。

However, that doesn't seem to be the case that you're running into. 但是,这似乎不是你遇到的情况。 In your case, you know that the two objects are of the same type. 在您的情况下,您知道这两个对象属于同一类型。 In C++, you will get compiler errors if a class you pass as a type parameter to a template does not support all the methods or operators that the template needs. 在C ++中,如果作为类型参数传递给模板的类不支持模板所需的所有方法或运算符,则会出现编译器错误。 That's what you're running into. 这就是你遇到的。 That's also the problem that concepts are meant to solve (well, concepts are meant to advertise "if you want to use your type with this template, then your type must support ..."). 这也是概念要解决的问题(好吧,概念是为了宣传“如果你想在这个模板中使用你的类型,那么你的类型必须支持......”)。 But, unfortunately we didn't get concepts in C++11, so the requirements are implicit. 但是,遗憾的是我们没有在C ++ 11中获得概念,因此需求是隐含的。 In your case, as already mentioned, you simply need to make sure that whatever class you pass in as Dominio supports operator== . 在您的情况下,如前所述,您只需要确保您传入的任何类作为Dominio支持operator==

You may also want to look at Boost concept check to advertise that whatever type is passed in as Dominio must support operator== . 您可能还想查看Boost概念检查,以宣传传入的任何类型,因为Dominio必须支持operator==

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

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