I need to store different types of objects in a single container. All the objects that go in the container will have a void continueExecution()
function with the same purpose, but the implementation is totally different.
I found this question to be very helpful Objects of different classes in a single vector?. I tried to implement the answer, but c++ is crushing me. Can you please suggest how to call the derived functions rather than the base functions after getting the object from std::vector
? I was really hoping that any calls to base.continueExecution()
would call derived.continueExecution()
. please see below:
Base class: - I created a base class strictly to make all my classes with the same function name be the same type. I will never create an object out of this class. The base class header file:
class DelayCommandBase
{
public:
DelayCommandBase();
void continueExecution();
};
The cpp file for the base class is just a print statement, and if it’s running that is bad for me (and it is running):
void DelayCommandBase::continueExecution()
{
Serial.println("DelayCommandBase continueExecution function... why is c++ so hard?!");
}
Derived class - I have a bunch of these types of files, lets stick to tst.h with a TST class. :
class TST : public DelayCommandBase
{
// …code...
public:
void continueExecution();
//…code...
};
The derived class .cpp file is implementing the continueExecution()
and a bunch of other functions. I don't think the issue is there.
A third class - this is what manages many objects that are constructed using the derived classes. In the header file I am creating a vector of base type. I think this is how I can store all the derived classes in a single container (assuming I don't use boost, which I don't want to use). Header file:
private:
std::vector<DelayCommandBase> midFlightCommandObjects;
In the .cpp file (all in a single function) an object is created from a derived class, is started via a function call, and is stored:
int hardwarePin = 13;
int delay = 1000;
TST tstCommandObj(hardwarePin, delay); //a working object... Yay
//the object is executed, and all is so happy so far:
tstCommandObj.start();
//Then I store it in the vector of base type:
if(tstCommandObj.commandIsFinished() == false)
{
midFlightCommandObjects.push_back(tstCommandObj);
Serial.println("TST object saved");
}
So far no issues. Now in a different function, in the same manager class I try the following:
midFlightCommandObjects.at(i).continueExecution();
and the function in the base class runs, not the derived class with the implementation. I tried making the function virtual
, but that didn’t fly. I tried using →
instead of .
but that gave errors that bend my mind. I tried passing the derived class as a pointer to the base class object and that didn't do anything different. I am not a programmer, and c++ is crushing my belief that I am a competent human. What do I need to do to call the derived class instead of the based class once I pull the objects out of the container?