ROOT 中的 ClassImp 预处理器宏 - 真的需要吗?

ClassImp preprocessor macro in ROOT - Is it really needed?

我真的必须使用 ClassImp 宏才能在 ROOT 中自动生成字典和流媒体吗?一些在线教程和示例提到了它,但我注意到只需将 ClassDef(MyClass, <ver>) 宏添加到 MyClass.h 并使用 rootcint/rootcling 处理它已经生成了大部分此类代码。

我确实看过 Rtypes.h 定义这些宏的地方,但是要遵循预处理器宏相互调用并不容易,因此,如果专家能够确认 ClassImp 的作用,那就太好了。我对最近版本的 ROOT >= 5.34

特别感兴趣

你到底想做什么? ClassImpClassDef 宏将成员添加到提供 Run-Time Type Information 的 class 并允许将 class 写入根文件。如果您对此不感兴趣,那么请不要理会这些宏。

我从不使用它们。

这是我在 roottalk 邮件列表上得到的答案,确认 ClassImp 的使用基本上已经过时了。

ClassImp is used to register in the TClass the name of the source file for the class. This was used in particular by THtml (which has now been deprecated in favor of Doxygen). So unless you code/framework needs to know the name of the source files, it is no longer necessary to have ClassImp.

ClassDef is necessary for class inheriting from TObject (or from any classes that has a ClassDef). In the other cases, it provide accelerator that makes the I/O slightly faster (and thus is technically not compulsory in this case). It also assign a version number to the schema layout which simplifies writing schema evolution rules (on the other hand, there is other alternative to assign a version number to the schema layout).