c++ - 在 std::thread 构造函数中调用派生类的函数最终调用基类的函数
问题描述
class Decoder {
public:
virtual void run() {}
//...
}
现在,我有这个派生类:
class NVDecoder : public Decoder
{
public:
void run();
在 NVDecoder.cpp 文件中定义为 this
void NVDecoder::run()
{
//...
}
由于函数 void Decoder
is virtual
,执行:
//Decoder decoder = new NVDecoder();
auto decoder = std::make_shared<NVDecoder>(NVDecoder::NALU,Decoder::H264);
decoder->run();
应该调用NVDecoder
' 的运行函数,而不是Decoder
' 的。但是,当我为通用Decoder
对象启动线程时,我需要这样做:
auto decoderThread = std::make_shared<std::thread>(&Decoder::run, decoder);
这调用了Decoder
'run 函数,而不是NVDecoder
' 函数。我想是因为我通过了&Decoder::run
。如何在不通过的情况下启动使用NVDecoder
'srun
函数的线程&NVDecoder::run
?
解决方案
推荐阅读
- angular - Angular2 v6 - ng-snotify
- git - 如何解决在 git 中有两个父母的问题
- php - 提交未确认 html php
- php - php array_intersect 和 array_intersect_assoc 函数用于关联数组显示错误
- linux - 防止变量覆盖到 submakefile
- django - Django Rest Framework 以一对多关系获取其他相关字段
- windows - Windows防火墙意外重置连接
- asp.net - 如何向指向一个端点的 Swashbuckle 文档路径添加多个操作?
- c# - NuGet 引用存储在哪里?
- matlab - 计数和分配给变量的提示