
在程式设计中,所谓的执行期型态讯息(Runtime type information,RTTI)指的是在程式执行时保存其物件的型态讯息的行为。某些语言实作仅保留有限的型态讯息,例如继承树资讯,而某些实作会保留较多资讯,例如物件的属性及方法讯息。
执行期型态讯息是一个电脑术语,用以标示一个电脑语言是否有能力在执行期保持或判别其物件或变量的型态讯息。
虽然执行期型态讯息是一个通用的电脑术语,但是通常用来称呼C++的特质。为了让C++的指令达到動態指派(dynamic_cast)、型態識別碼(typeid)操作与例外处理的能力,执行期型态讯息是必须的。
下列是一C++的执行期型态讯息应用范例:
class base {
virtual ~base(){}
};
class derived : public base {
public:
virtual ~derived(){}
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));
}
Why are we here?
All text is available under the terms of the GNU Free Documentation License
This page is cache of Wikipedia. History