samedi 12 janvier 2019

How to create the derived object copy when the object passed as Base type? C++

I'm having this design right now: (Notice IWorker is an abstract class)

UML diagram

as you can see the function "addWorker" is pure virtual and implemented in the lower class Manager.

My problem is here:

void Manager::addWorker(IWorker* worker) {
    this->_workers.push_back(worker);
    worker->setBoss(this);
}

1) The compiler can't create IWorker object for the vector _workers. What is the right way to solve this problem? I thought to go with prototype design and force all IWorker objects to implement copy() method and use it there.

2) Is it good for IWorker to hold _boss as an IWorker object or should I try pointers approach as IWorker* ?

Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire