E D R , A S I H C RSS

Run Time Type Information



1. RTTI

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

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

2. example

아래는 RTTI의 쓰임새를 표현한 일예이다.
~cpp
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));
}

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

3. RTTI in MFC


MFC에서 CRuntimeClass 구조체, DECLARE_DYNAMIC, IMPLEMENT_DYNAMIC, DECLARE_DYNCREATE, IMPLEMENT_DYNCREATE, RUNTIME_CLASS 를 이용해서 구현하고 있다.
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:56
Processing time 0.0185 sec