CreateFileMapping 与包含虚拟方法的类
我想创建一个类的实例并将其放置在共享内存中,以便可以从多个进程调用同一个实例。然而,这个类有虚方法,我认为这可能会导致问题,因为我已经读到映射数据不能包含指针,类中的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 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.