首页 > 解决方案 > 在 std::thread 构造函数中调用派生类的函数最终调用基类的函数

问题描述

class Decoder    {
public:
    virtual void run() {}
    //...
}

现在,我有这个派生类:

class NVDecoder : public Decoder
{
public:
    void run();

在 NVDecoder.cpp 文件中定义为 this

void NVDecoder::run()
{
//...
}

由于函数 void Decoderis 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

标签: c++

解决方案


推荐阅读