Ver Mensaje Individual
  #2 (permalink)  
Antiguo 29/11/2012, 01:52
fightmx
 
Fecha de Ingreso: febrero-2003
Ubicación: D.F.
Mensajes: 163
Antigüedad: 21 años, 2 meses
Puntos: 22
Respuesta: Como hacer que typeid retorne el tipo del objeto 'hijo'

Para ese caso tendrías que hacerlo así (utilizo struct por efectos prácticos):

Código C++:
Ver original
  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;
  4.  
  5. struct Component{
  6.     virtual void printUniqueName(){
  7.         cout << typeid(*this).name() << endl;
  8.     }
  9. };
  10.  
  11. struct Cube : Component{
  12.  
  13. };
  14.  
  15. int main(){
  16.     Component com;
  17.     Cube cub;
  18.  
  19.     com.printUniqueName();
  20.     cub.printUniqueName();
  21. }

OJO: Si tratas de invocar a la función virtual desde el constructor no te va a funcionar.