[SSM 안드로이드 프레임워크 개발 강의]25. RTTI 이야기
#include<iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// RTTI 이야기
// 1. 가상함수 테이블로 관리되는 type_info 를 얻어서 조사한다.
// 2. typeid(*p) == typeid(Dog) 로 조사한다.
// 3. Dog *p1 = dynamic_cast<Dog *>(p); C# Dog p1 = p as Dog;
//
//
/////////////////////////////////////////////////////////////////////
/*
class Animal{
public:
virtual ~Animal(){}
};
class Dog : public Animal
{
};
void foo(Animal *p) // 모든 동물이 전달 될 수 있다.
{
//ㄹ/결국 아래 코드 표현을 사용하면됩니다.
if(typeid(*p) == typeid(Dog)){
}
//모든 동물에 공통된 일을 하겠습니다.
// 그런데 혹시 P가 Dog 라면 다른 일도 하고싶다!
//P가 Dog 인지 알고싶다 - Run Time Type Information : RTTI
//
//가상 함수 테이블로 관리되는 type_info를 얻어낸다.
const type_info& t1 = typeid(*p); //typeid (객체)
const type_info &t2 = typeid(Dog); //typeid(클래스 이름)
cout << t1.name() << endl;
if(t1==t2){
cout << " p는 Dog 입니다."<<endl;
}
cout << t1.name()<<endl;
}
int main(){
Animal a; foo(&a);
Dog d; foo(&d);
}
*/
class Animal{
public:
virtual ~Animal(){}
};
class Dog : public Animal
{
};
void foo(Animal *p){
//Dog *p1 = static_cast <Dog *> (p); //Down cast (부모를 자식포인터로 캐스팅) 을 조사못한다.
//Dog *p1 = dynamic_cast<Dog *>(p); //Down cast 발생시 0이 된다.
cout << p1 << endl;
}
int main(){
Animal a; foo(&a);
Dog b; foo(&d);
}