1

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 ?

Nextar
  • 1,088
  • 2
  • 12
  • 26
  • 2
    `At the moment a copy is returned as I wanted to first write the program and then learn how to correctly handle the memory.` Then there _is_ no derived class at runtime. Google the term object slicing. – tkausl Oct 14 '18 at 14:32
  • 1
    Take an OOP approach and use polymorphism and [virtual functions](https://en.cppreference.com/w/cpp/language/virtual) to define different behaviour for your derived events’ functions. – Santiago Varela Oct 14 '18 at 14:36
  • So if I use `Event` as return type from `fetch_event` the event will always be sliced to `Event` ? @tkausl Not sure how virtual functions will help me in this case. In my case the structs are just DTOs. And the processor should be called with the correct method to handle a DTO. @SantiagoVarela – Nextar Oct 14 '18 at 14:50

1 Answers1

0

Firstly if the design is c++ idiomatic at all (If not I would be happy to get hints)?

No, it's not.

The purpose of inheritance is that the consumer do not have to care where the object is a descendant or not. Here for your example, the processor should not care about is the event an instance of Event or Derived1, even more, the processor should not know if their are Derived1 and Derived2.

You have to hide these difference to Processor in your Derived1/Derived2 implementation, e.g.

// In C++, class and struct have no difference other than the default visibility

class Event{
  public:
    void call() {/*Do something*/}
}

class Derived1: public Event{
  public:
    void call() {/*Do something different*/}
}

class Derived2: public Event{
  public:
    void call() {/*Do something different*/}
}

void Processor::process(Event &event){
  // ...
  event.call();
  // ...
}
Galaxy
  • 1,129
  • 11
  • 27