Pure Virtual Method Called

后端 未结 7 1073
盖世英雄少女心
盖世英雄少女心 2021-02-08 10:24

EDIT: SOLVED

I\'m working on a multi-threaded project right now where I have a base worker class, with varying worker classes that inherit from it. At r

7条回答
  •  北恋
    北恋 (楼主)
    2021-02-08 11:00

    The problem appears to be that Director::manageWorker is called in the constructor of workerVariant instances:

    Director::manageWorker(baseWorkerClass* worker) {
        workerPtrArray[worker->getThreadID()] = worker;
    }
    

    Presumably getThreadID() isn't a pure virtual function or you would have (hopefully!) gotten a compiler error about not overriding it in workerVariant. But getThreadID() might call other functions which you should override, but are being invoked in the abstract class. You should double check the definition of getThreadID() to make sure you're not doing anything untoward that would depend on the child class before it's properly initialized.

    A better solution might be to separate this sort of multi-stage initialization into a separate method, or to design Director and baseWorkerClass such that they don't have this sort of initialization-time interdependency.

提交回复
热议问题