Ver Mensaje Individual
  #13 (permalink)  
Antiguo 30/06/2016, 16:31
dehm
 
Fecha de Ingreso: septiembre-2010
Mensajes: 494
Antigüedad: 13 años, 7 meses
Puntos: 10
Respuesta: Como declarar un tipo

Para entender todo lo que me has puesto y ver más en profundidad el tema de plantillas y herencias, he estado un rato leyendo este artículo que para mi está siendo bastante asequible:
http://www.codeproject.com/Articles/...mplates-Part-1

http://www.codeproject.com/Articles/...Templates-Part

(Voy por la parte 1)
Entonces habla de como implementar el constructor copia en una clase con plantilals, con este ejemplo:

Código C++:
Ver original
  1. Pair(const Pair<Type1, Type2>& OtherPair) :
  2.   first(OtherPair.first),
  3.   second(OtherPair.second)
  4.  {}

Además advierte expresamente que no poner los tipos dará un error:

Please note that it is very much required to specify template type arguments of Pair<>, for the argument of this copy-constructor. The following specification wouldn't make sense, since Pair is not a non-template type:

Código C++:
Ver original
  1. Pair(const Pair& OtherPair) // ERROR: Pair requires template-types

Pero yo acabo de hacerlo "mal" a ver qué pasa y me funciona:
Código C++:
Ver original
  1. template<class T1, class T2>
  2. struct pareja
  3. {
  4.     T1 first;
  5.     T2 second;
  6.  
  7.     //constructor
  8.     pareja():first(T1()),second(T2()) {}
  9.     pareja(const T1&t1,const T2&t2):first(t1),second(t2) {}
  10.     pareja(const pareja& p) //<----aqui lo estoy haciendo mal pareja(const pareja<T1,T2>& p)
  11.     {
  12.         first=p.first;
  13.         second=p.second;
  14.     }
  15.     bool operator == (const pareja<T1,T2>& Other) const
  16.     {
  17.         return first == Other.first &&
  18.                second == Other.second;
  19.     }
  20. };

¿Sabéis por qué es así?
Gracias como siempre!
__________________
Mi calculadora en Qt