I'm actually pretty new to C++ so I'm still trying to learn the basics
I have events. A base one and derived ones.
struct Event {};
struct Derived1 : Event {};
struct Derived2: Event {
public:
RoundResult result;
};
...
Event fetch_event(...) {...}
Some method returns these events. At the moment a copy is returned as I wanted to first write the program and then learn how to correctly handle the memory.
In my program there is another part that gets these events and passes it to a class that processes the events.
...
auto event = y->fetch_event(...);
x->process(event);
...
So right now I have the following question. I want to call different process methods based on the derived event struct I got.
void Processor::process(Event event) {
...
}
void Processor::process(Derived1 event) {
...
}
void Processor::process(Derived2 event) {
...
}
Right now always process(Event event)
is called. I've seen there there is some kind of instanceof
like in java but I'm actually not sure if this is really idiomatic in c++.
So my question is. Firstly if the design is c++ idiomatic at all (If not I would be happy to get hints)? And if it is: How can I achieve that the called method is based on the derived class on runtime ?