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


0