C++ 类的运行时知识

发布于 2024-07-06 10:47:30 字数 132 浏览 3 评论 0原文

我有多个类,它们都派生自基类,现在某些派生类将不会根据平台进行编译。 我有一个类,允许我返回基类的对象,但是现在派生类的所有名称都已被硬编码。

有没有办法确定哪些类已编译(最好在运行时),以便我可以删除链接并提供动态可加载库。

I have multiple classes that all derive from a base class, now some of the derived classes will not be compiled depending on the platform. I have a class that allows me to return an object of the base class, however now all the names of the derived classes have been hard coded.

Is there a way to determine what classes have been compiled, at run-time preferably, so that I can remove the linking and instead provide dynamically loadable libraries instead.

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

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

发布评论

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

评论(7

小矜持 2024-07-13 10:47:30

您是否正在寻找 C++ 运行时类注册? 我找到了这个链接备份)。

这可能会实现你想要的,我不确定动态加载的模块以及是否可以使用相同的方法注册它们。

Are you looking for C++ runtime class registration? I found this link (backup).

That would probably accomplish what you want, I am not sure about the dynamically loaded modules and whether or not you can register them using the same method.

因为看清所以看轻 2024-07-13 10:47:30

我不知道您真正想要完成什么,但您可以在每个派生类的实现文件中放置一个单例构造函数,将名称以及指向工厂的指针添加到列表中。 然后该列表始终是最新的并且可以创建所有已编译的类。

I don't know what you're really trying to accomplish, but you could put a singleton constructor in each derived class's implementation file that adds the name to a list, along with a pointer to a factory. Then the list is always up to date and can create all the compiled in classes.

咿呀咿呀哟 2024-07-13 10:47:30

一般来说,在 C++ 中依赖运行时类型信息是一个坏主意。 你所描述的似乎是工厂模式。 您可能需要考虑为每个平台创建一个特殊的工厂子类,它只知道该平台上存在的类。

Generally, relying on the run-time type information is a bad idea in C++. What you have described seems like the factory pattern. You may want to consider creating a special factory subclass for each platform, which would only know about classes that exist on that platform.

倒带 2024-07-13 10:47:30

如果每个类都有自己的动态库,只需检查该库是否存在即可。

If every class has its own dynamic library, just check if the library exists.

秋千易 2024-07-13 10:47:30

这听起来像是一个使用“编译时多态性”或模板策略参数的地方。

请参阅 Andrei Alexandrescu 的《现代 C++ 设计》以及他基于本书的 Loki 实现。 另请参阅维基百科上的 Loki 页面。

This sounds like a place to use "compile time polymorphism" or template policy parameters.

See Modern C++ Design by Andrei Alexandrescu and his Loki implementation based on the book. See also the Loki page at wikipedia.

策马西风 2024-07-13 10:47:30

有一些讨厌的、特定于编译器的技巧可以在运行时获取类信息。 相信我,你不会想打开那罐蠕虫。

在我看来,唯一认真的方法是对每个派生类使用条件编译。 在#ifdef 块中,定义一个new 常量,其中包含正在编译的类名。 然后,名称仍然是硬编码的,但全部位于中心位置。

There are nasty, compiler-specific tricks for getting at class information at runtime. Trust me, you don't want to open that can of worms.

It seems to me that the only serious way of doing this would be to use conditional compilation on each of the derived classes. Within the #ifdef block, define a new constant which contains the class name which is being compiled. Then, the names are still hard coded, but all in a central location.

阳光的暖冬 2024-07-13 10:47:30

派生类的名称必须用 C++ 进行硬编码。 没有其他方法可以使用它们。 因此,不仅没有办法自动检测哪些类已被编译,而且如果该信息存在,也没有办法使用它。

如果您可以根据名称在运行时指定类,例如:

std::string foo = "Derived1";
基*对象=新的“foo”; // 或任何您喜欢的符号 - 在 C++ 中不起作用,

那么判断“Derived1”是否已编译的能力将很有用。 由于您必须直接指定类,例如:

Base * object = new Derived1; // 在 C++ 中确实有效,

所有检查都是在编译时完成的。

The names of the derived classes have to be hard-coded in C++. There's no other way to use them. Therefore, not only is there no way to automatically detect what classes have been compiled, there would be no way to use that information if it existed.

If you could specify classes at run-time based on their name, something like:

std::string foo = "Derived1";
Base * object = new "foo"; // or whatever notation you like - doesn't work in C++

then the ability to tell if "Derived1" was compiled or not would be useful. Since you have to specify the class directly, like:

Base * object = new Derived1; // does work in C++

all checking is done at compile time.

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