Pure Virtual Method Called

后端 未结 7 1072
盖世英雄少女心
盖世英雄少女心 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 10:45

    I didn't see the variant class being constructed in any of your code samples. Are you sure the id being passed is within range for the worker array? Also, you're constructing the objects using 'new', right? If you constructed the object on the stack, it would register itself with the Director, but after the constructor returns the object will be immediately destroyed, but the Director will retain it's pointer to the object that was on the stack.

    Also, your baseWorkerClass destructor should be virtual along with the workerVariant destructor to make sure they get called when you delete the array of baseWorkerClass.

    From my comment to another question, consider using std::vector instead of the double pointer. It's easier to maintain and understand and removes your need to maintain the array.

    It seems like you've added an unnecessary layer of abstraction here. I don't think the id should really be part of the subclass interface. I think something like this might work better for you:

    class baseWorkerClass
    {
    public:
    
        baseWorkerClass(int id) :
            id( id )
        {
        }
    
        virtual ~baseWorkerClass()
        {
        }
    
        int getThreadID(){ return id; };
        virtual int getSomeVariable() = 0;
    
    protected:
        int id;
    };
    
    class workerVariant : protected baseWorkerClass
    {
        public:
    
        workerVariant(int id) :
            baseWorkerClass( id )
        {
            Director::manageWorker(this);
        }
    
        virtual ~workerVariant()
        {
        }
    
        int getSomeVariable()
        {
            return someVariable;
        }
    
    protected:
        int someVariable
    };
    

提交回复
热议问题