How to compare two typical types in C ++?

I need to determine if the element is the same as the one I pass by reference. In the function, BelongsI need to compare the equality between dis and the element stored in the 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;
}
+5
source share
3 answers

Regardless of the type argument you specify for the type parameter Dominio, you must overload operator==for that type.

Suppose you write this:

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

then you should overload operator==for a type Alike:

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

, public, -, const, const A.

, -, operator== , :

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

.

+4

operator== :

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

, , WhateverType.

+2

- , , , , , Boost , TR1 ++ 11 ( , TR1 ++ 11).

, . , . ++ , , , , . , . , (, ", , ..." ). , , ++ 11, . , , , , Dominio, operator==.

You can also look at "Boost Concept Testing" to declare that any type passed in as quality Dominioshould support operator==.

+1
source

All Articles