简体   繁体   中英

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:

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.

Suppose, you write this:

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

then you've to overload operator== for the type A as:

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 .

Instead of making it member function, you can make operator== a non-member function as well:

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:

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

or maybe as a member of 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).

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. 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. In your case, as already mentioned, you simply need to make sure that whatever class you pass in as Dominio supports operator== .

You may also want to look at Boost concept check to advertise that whatever type is passed in as Dominio must support operator== .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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