C/Visual C - Otra de templates

 
Vista:

Otra de templates

Publicado por Nelek (816 intervenciones) el 23/02/2007 15:50:36
Hola a todos,

Como ya dije en un mensaje anterior, estoy ampliando la plantilla (template) de "CMyList" que me baje de una pagina de recursos para VC++. Ya postee la primera funcion que le anyadi, y aqui va la segunda. Espero que os sea de utilidad y acepto cualquier tipo de sugerencia/mejora para ella.

// Operator == (CMyList1 == CMyList2)
template <class TYPE, class ARG_TYPE>
BOOL CMyList<TYPE, ARG_TYPE>::operator== (const CMyList &temp)
{
ASSERT_VALID (this);
ASSERT_VALID (temp);
//Check for self assignment
if(this == &temp)
return TRUE;

//If the other list has different number of elements, they can't be equal
if (GetCount () != temp.GetCount ())
return FALSE;

int nError = 0;
POSITION posThis = GetHeadPosition ();
ASSERT (posThis);
POSITION posOther = temp.GetHeadPosition ();
ASSERT (posOther);
while ((posThis)&&(posOther))
{
//This is to look for in the same place in both lists
TYPE thisTYPE = (TYPE)GetNext(posThis);
TYPE otherTYPE = (TYPE)temp.GetNext(posOther);

//This template presupposes that the TYPE object has the operator==
if (thisTYPE == otherTYPE)
;
//If both TYPE elements are equal, nothing. If not, increase nError
else
nError++;
}
//There is no errors, the lists are equal
if (nError == 0)
return TRUE;

//Default return = NOT EQUAL
return FALSE;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Otra de templates

Publicado por Tom (17 intervenciones) el 23/02/2007 19:48:42
A mí sólo se me ocurre una cosilla, tras un vistazo rápido ...
// int nError = 0; No me hace falta
...

while(posThis) {
//This is to look for in the same place in both lists
TYPE thisTYPE = (TYPE)GetNext(posThis);
TYPE otherTYPE = (TYPE)temp.GetNext(posOther);

if(! thisTYPE == otherTYPE) {
break; // No necesito seguir comprobando
}
}
if(posThis) // Si posThis no es null, el bucle ha finalizado por diferencias
return FALSE;

return TRUE;
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Otra de templates

Publicado por Nelek (816 intervenciones) el 26/02/2007 07:34:21
Mmmm, tienes razon, no habia caido en eso. Al llegar al final el puntero es nulo, por eso el while (pos), asi que se puede tomar el propio puntero para saber si ha acabado la lista o no. Gracias por la mejora
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar