Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

RunTimeTypeInformation

From ZeroWiki
(Redirected from RTTI)

RTTI

동적으로 만들어진 변수의 타입을 비교하고, 특정 타입으로 생성하는 것을 가능하게 한다. (자바에서는 instanceof를 생각해보면 될 듯)

MFC에서는 C++에서 표준으로 이용되기 이전부터 매크로를 이용한 방법으로 이것을 지원했으며, 아직도 그 잔재가 남아있다.

example

아래는 RTTI의 쓰임새를 표현한 일예이다.

class base {
};

class derived : public base {
   int compare(derived &ref);
};

int my_comparison_method_for_generic_sort(base &ref1, base &ref2)
{
    derived d = dynamic_cast<derived &>(ref1);  // rtti used here
    // rtti enables the process to throw a bad_cast exception
    // if the cast is not successful
    return d.compare(dynamic_cast<derived &>(ref2));
}

위에서 보듯이 클래스의 형을 비교하는 것이 가능하다.

RTTI in MFC

MFC에서 CRuntimeClass 구조체, DECLARE_DYNAMIC, IMPLEMENT_DYNAMIC, DECLARE_DYNCREATE, IMPLEMENT_DYNCREATE, RUNTIME_CLASS 를 이용해서 구현하고 있다.