使用队列进行对象切片
Object slicing with Queues
使用带有-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
。它是如何实现的取决于您尚未显示的代码。
相关文章:
- 避免矢量中的对象切片<Base><shared_ptr>
- 专门化模板覆盖函数/避免对象切片
- C++ 被此代码与多态性、指针和对象切片混淆
- 我们能胜过对象切片吗?
- 如何在考虑对象切片的同时通过传入单个 Base 对象来打印出数组中的对象?
- 成员变量和对象切片
- 如何在不引入未来对象切片的情况下实现 ICloneable
- 避免对象切片
- 故意对对象切片是一种可行的技术吗?
- 对象切片:通过按值派生为 Base - 安全还是危险?
- C++ CRTP 派生类对象切片
- C++ - 即使在使用指针后也能进行对象切片
- 了解对象切片
- 使用对象切片可靠地从多个基类之一中复制
- 对象切片和隐式类型转换
- 一种克服对象切片的方法
- 按引用传递可避免对象切片
- 使用CRTP时的对象切片
- 对象切片:从基类对象访问分层的类方法
- 这会导致对象切片吗?