有没有办法在 C++ 中获取基类名称?在运行时?

发布于 2024-10-05 22:25:35 字数 107 浏览 0 评论 0原文

假设 RTTI 已启用,您可以在运行时使用 typeid(this).name() 获取当前类名称。

我希望能够获得“this”的基类名称。这可能吗?我没有使用多重继承,以防产生影响。

You can get the current class name, assuming RTTI is enabled, using typeid(this).name() at runtime.

I would like to be able to get the name of the base class for "this". Is that possible? I'm not using multiple inheritance, in case that makes a difference.

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

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

发布评论

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

评论(4

眼藏柔 2024-10-12 22:25:35

另外,我认为您不能指望 typeid(this).name() 为您提供类名(据我所知,要返回的确切值是实现定义的)。

Plus, I don't think that you can count on typeid(this).name() to give you a class name (AFAIK the exact value to be returned is implementation-defined).

梨涡 2024-10-12 22:25:35

不,先生。对不起。以这种方式获取类名的方法取决于实现。在我的实现中,我得到了损坏的名称。

No sir. Sorry. And your method of getting the class name this way is implementation-dependent. On my implementation, I get the mangled name.

暮凉 2024-10-12 22:25:35

无法可靠地检索信息,因为它没有可靠地存储在任何地方,反过来又因为 C++ 哲学不会为您提供任何东西,除非您明确要求它们。

The information can't reliably be retrieved because it isn't reliably stored anywhere, in turn because the C++ philosophy is not to give you things unless you explicitly ask for them.

请爱~陌生人 2024-10-12 22:25:35

不,事实并非如此,而且您需要此功能的原因值得怀疑:)

No, it is not, and the reason you need this feature is dubious :)

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