显式类型标识符与 RTTI

发布于 2024-11-09 18:56:57 字数 251 浏览 1 评论 0原文

与 RTTI 相比,使用自己的类型标识符有什么优势吗?

例如

class A { virtual int mytype() = 0; };
class B : public A { int mytype() {return 1;} };
class C : public A { int mytype() {return 2;} };

可以更快吗?更少的开销?或者在这种情况下应该始终使用 RTTI 吗?

is there any advantage of using your own type identifier over RTTI?

e.g.

class A { virtual int mytype() = 0; };
class B : public A { int mytype() {return 1;} };
class C : public A { int mytype() {return 2;} };

Could it be faster? Less overhead? Or should one always use RTTI in such a situation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请别遗忘我 2024-11-16 18:56:57

测试之前,不要假设 RTTI 的开销会比您的解决方案更多/更少。

您应该尝试这两种解决方案并测量性能以获得可靠的答案。

实际上,几年前我问了自己同样的问题,最后我添加了一个成员变量来“紧固”类型测试,就像您所做的那样。结果我的代码不必要地混乱了愚蠢的测试,而一些 dynamic_cast<> 可以完成相同的工作(事实上,更好的工作)。

从那时起,我重构了代码以使用 dynamic_cast<>,并且我不会再回去了。

作为脚注:如果您的类是多态的,那么您已经为此“付费”了,因此只需使用 dynamic_cast<> 即可。

Don't assume that RTTI will have more/less overhead than your solution before testing it.

You should try both solutions and measure the performances to get a reliable answer.

I actually asked myself the same question a few years ago and I ended up adding a member variable to "fasten" the type testing, just like you did. Turned out my code was needlessly cluttered with stupid tests while some dynamic_cast<> would have done the same job (in fact, a better job).

I refactored the code to use dynamic_cast<> since then and I wouldn't go back.

As a foot-note: if your classes are polymorphic, you already "paid" for this anyway, so just go with dynamic_cast<>.

清风疏影 2024-11-16 18:56:57

使用自定义类型标识符的缺点(对于多态类型)是:

  1. 需要记录每个
    类继承。你需要分配
    唯一的整数或枚举值
    给定层次结构中的所有类
  2. 说出你的垂直方向
    继承就像A->B->D
    对于诸如 A *p = new D; 的情况,自定义类型标识将不允许将 B*p 匹配(即使它是有效的) )。

您需要了解这些情况。
另一方面,

  1. RTTI 仅适用于
    多态类型(所以
    继承链不包含
    虚拟功能无法利用RTTI)
  2. 有一点性能
    RTTI 导致差异减小,如果确实如此
    对你来说很重要

但是,正如你在评论中提到的,对于较小的继承链,跟踪你自己的类型没有什么坏处。例如

struct Base {
  enum TYPES { _BASE, _D1, _D2, _D3 };
  const TYPES &myType;
  Base (TYPES) : myType(_BASE) {}
};

struct D1 : Base {
  D1 () : Base(_D1) {}
};

The disadvantages (for polymorphic types) with custom type identifier are:

  1. One needs to keep record of every
    class inherited. You need to assign
    a unique integer or enum value for
    all the classes in a given hierarchy
  2. Say your vertical
    inheritance is like, A->B->D.
    For situations like, A *p = new D; the custom type identification will not allow to match B* with p (even though it's valid).

You need to be aware of these situations.
On the other had,

  1. RTTI is applicable to only
    polymorphic types (so the
    inheritance chain not containing
    virtual functions cannot leverage RTTI)
  2. There is a little performance
    difference decrease due to RTTI, if it really
    matters to you

But, as you mentioned in your comment, for smaller inheritance chain, there is no harm in keeping track of your own typing. e.g.

struct Base {
  enum TYPES { _BASE, _D1, _D2, _D3 };
  const TYPES &myType;
  Base (TYPES) : myType(_BASE) {}
};

struct D1 : Base {
  D1 () : Base(_D1) {}
};
梦一生花开无言 2024-11-16 18:56:57

http://en.wikibooks.org/wiki/C%2B%2B_Programming/RTTI

RTTI 有一些限制。首先,RTTI 只能与多态类型一起使用。这意味着您的类必须至少有一个虚函数,无论是直接的还是通过继承的。其次,由于存储类型需要额外的信息,一些编译器需要特殊的开关来启用 RTTI。

因此,如果您需要它在没有虚函数的类上工作,您必须自己实现它。

http://en.wikibooks.org/wiki/C%2B%2B_Programming/RTTI

There are some limitations to RTTI. First, RTTI can only be used with polymorphic types. That means that your classes must have at least one virtual function, either directly or through inheritance. Second, because of the additional information required to store types some compilers require a special switch to enable RTTI.

So, if you need it to work on classes without virtual functions, you'd have to implement it yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文