使用队列进行对象切片

Object slicing with Queues

本文关键字:对象 切片 队列      更新时间:2023-10-16

使用带有-std=c++0x的Eclipse/gcc在Ubuntu上开发。

我似乎对对象切片有问题,这不属于我在这里看到的其他问题。我有一个非常简单的基类/子类继承模型。基类有一个纯虚拟函数,显然是子类实现的:

class Parent{
public:
    Parent();
    virtual ~Parent();
    virtual void DoStuff() = 0;
};
class Child : public Parent{
public:
    Child();
    virtual ~Child();
    virtual void DoStuff(); //Child can have "grandchildren"
};

我想要的是有一个队列,在那里我可以存储这些对象以供工作线程处理。我知道我应该存储指针,否则我保证切片。所以,在做这件事的类("处理器")中,我有:

typedef queue<Parent*> MYQUEUE; //#include <queue>
static MYQUEUE myQueue;
//Call this after creating "Child c;" and passing in &c:
void Processor::Enqueue(Parent* p) 
{
    myQueue.push(p);
}
void* Process(void* args) //function that becomes the worker thread
{
    while(true)
    {
        if(!myQueue.empty())
        {
            Parent* p = myQueue.front();
            p->DoStuff();
            myQueue.pop();
         }
    }
    return 0;
}

然后发生的情况是程序崩溃,说"调用了纯虚拟方法",就好像继承/多态性没有正常工作一样。我知道继承设置正确,因为在测试时我确认了这一点:

Child c;
Parent* p = &c;
p->DoStuff();

非常感谢任何指导!

如果将对象传递给工作线程,则无法在堆栈上创建它。当工作线程调用它时,父线程可能已经离开了该函数并销毁了对象。您需要在父线程中动态分配(可能通过new)它,并且在完成后只在工作线程中释放(delete)它

另一个注意事项是,如果父级能够在工作进程运行时将作业排入队列,则需要锁定队列访问。

此错误表示p在调用点指向的对象的类型为Parent。它是如何实现的取决于您尚未显示的代码。