CreateFileMapping 与包含虚拟方法的类

发布于 2024-12-25 06:26:23 字数 112 浏览 3 评论 0原文

我想创建一个类的实例并将其放置在共享内存中,以便可以从多个进程调用同一个实例。然而,这个类有虚方法,我认为这可能会导致问题,因为我已经读到映射数据不能包含指针,类中的 vtable 就是这种情况。它会起作用吗?

I want to create an instance of a class and place it in shared memory so the same instance can be called from multiple processes. However, this class has virtual methods which I think may cause problems as I have read the mapped data can't contain pointers, which would be the case here with the vtable in the class. Will it work?

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

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

发布评论

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

评论(1

伪心 2025-01-01 06:26:23

正如 Kerrek SB 评论的那样,您无法映射包含虚拟方法的类。但是您可能可以创建一个没有虚函数的简单结构或类,将其映射,然后将指向它的指针提供给另一个具有虚函数并使用普通结构作为其实现的类。基本上,Pimpl 惯用语

如果需要,您甚至可以通过在普通结构中存储“类型”整数并检查它来决定调用哪些函数来自己执行虚拟调度之类的操作。

As Kerrek SB commented, you cannot map a class containing virtual methods. But you can probably make a simple struct or class without virtuals, map that, and then give a pointer to it to another class which does have virtuals and uses the plain struct as its implementation. Basically, the Pimpl idiom.

If needed, you can even do something like virtual dispatch yourself by storing a "type" integer in the plain struct, and inspecting it to decide which functions to invoke.

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