避免dynamic_cast / RTTI [英] Avoiding dynamic_cast/RTTI

查看:117
本文介绍了避免dynamic_cast / RTTI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在为一个项目工作一段C ++代码( cpp-markdown library ,好奇),并遇到一个编码问题,我想征求一些意见。



cpp-markdown 有一个名为 Token 的基类,它有多个子类。其中两个主要子类是 Container (其中包含其他 Token 的集合)和 TextHolder (用作 Token 的基类,当然包含文本)。



大多数处理通过虚函数处理,但是一些处理更好地在单个函数中处理。为此,我最后使用 dynamic_cast 将指针从 Token * 下拉到其子类之一,所以我可以调用特定于子类及其子类的函数。因为代码能够通过虚函数(例如 isUnmatchedOpenMarker )来告诉这样的事情。



我可以用另外两种方法处理:


  1. > 的函数,我想将它们作为 Token 的虚函数调用,只需要为每个子类保留一个空体,

  2. 令牌中创建一个虚函数,该函数将正确返回当它在某些子类型上被调用时,类型指针指向 this ,如果在任何其他子类型上调用,则为空指针。


第二种方法似乎比现在一个和第一个,对我。但我想知道其他经验丰富的C ++开发人员的看法。或者我是否担心太琐碎。 : - )

解决方案

#1污染了不需要它的对象的类命名空间和vtable。当你有一些通常会被实现的方法,但只是一个单一的派生类只需要一个简单的方法。



#2只是 dynamic_cast<> 在圆点连衣裙和口红。不会使客户端代码更简单,并且纠缠整个层次结构,需要基础和每个派生类半知晓每隔一个派生类。



只需使用 dynamic_cast<> 。这是它的目的。


I was recently working on a piece of C++ code for a side project (the cpp-markdown library, for the curious), and ran into a coding question that I'd like some opinions on.

cpp-markdown has a base class called Token, which has a number of subclasses. Two of the main subclasses are Container (which holds collections of other Tokens) and TextHolder (used as a base class for Tokens that contain text, of course).

Most of the processing is handled via virtual functions, but some of it was better handled in a single function. For that, I ended up using dynamic_cast to down-cast the pointer from a Token* to one of its subclasses, so I could call functions that are specific to the subclass and its child classes. There's no chance the casting would fail, because the code is able to tell when such a thing is needed via virtual functions (such as isUnmatchedOpenMarker).

There are two other ways I could see to handle this:

  1. Create all of the functions that I want to call as virtual functions of Token, and just leave them with an empty body for every subclass except the one(s) that need to handle them, or...

  2. Create a virtual function in Token that would return the properly-typed pointer to this when it's called on certain subtypes, and a null pointer if called on anything else. Basically an extension of the virtual function system I'm already using there.

The second method seems better than both the existing one and the first one, to me. But I'd like to know other experienced C++ developers' views on it. Or whether I'm worrying too much about trivialities. :-)

解决方案

#1 pollutes the class namespace and vtable for objects that don't need it. Ok when you have a handful of methods that will generally be implemented, but plain ugly when only needed for a single derived class.

#2 is just dynamic_cast<> in a polka-dot dress and lipstick. Doesn't make client code any simpler, and tangles up the entire hierarchy, requiring the base and each derived class to be semi-aware of every other derived class.

Just use dynamic_cast<>. That's what it's there for.

这篇关于避免dynamic_cast / RTTI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆