c++ – Method not called in a class that inherits another

Question:

Hello everyone. I can't figure out why my code isn't working as intended.
Actually, here is the code itself:
EventManager class:


vector<Event> EventManager::getEvents() {
    return events;
}

void EventManager::addEvent(const Event& e) {
    if (!eventExists(e)) {
        events.push_back(e);
    }
}

bool EventManager::eventExists(Event e) {
    for (Event ev : events) {
        if (ev.getName() == e.getName()) {
            return true;
        }
    }
    return false;
}

EventHelper class:

void EventHelper::onConsoleMessage(string message) {
    for (Event e : EventManager::getEvents()) {
        e.onConsoleMessage(message);
    }
}

Event class:

Event::Event(string name) {
    this -> name = move(name);
}

void Event::onConsoleMessage(const string& message) {

}

string Event::getName() {
    return name;
}

Well, the class itself, which inherits the Event class:

ConsoleMessageEvent::ConsoleMessageEvent() : Event("test") {

}

void ConsoleMessageEvent::onConsoleMessage(const string &message) {
    std::cout << "test10" << std::endl;
}

Everything works great, but for some reason the method does not want to be executed when it is called through EventHelper .
The call itself:

int main() {
    EventManager::addEvent(ConsoleMessageEvent());
    log("Test");
    return 0;
}

void log(const string& message) {
    EventHelper::onConsoleMessage(message);
    cout << message << endl;
}

As planned, it should display the message test10 after using the log method in the main.cpp class. In java this code works fine. I'm completely new to C++ , so please explain where I made a mistake. And I also have a question: what is the alternative to Map and List in C++ , about the List list I found information about vector, but I think that there is a much more convenient option. If necessary, I can attach .h files (I wasn't sure they would be useful here, so I attached only .cpp ).
Event.h class:

class Event {
private:
    string name;
public:
    explicit Event(string name);
    string getName();
    virtual void onConsoleMessage(const string& message);
};

ConsoleMessageEvent.h class:

class ConsoleMessageEvent : public Event {
public:
    ConsoleMessageEvent();
    void onConsoleMessage(const string &message) override;
};

I tried to do it in another project and the result is the same, the method is simply not called.

#include <iostream>
#include <utility>
#include "vector"

using namespace std;

class Event {
private:
    string name;
public:
    explicit Event(string name) {
        this -> name = move(name);
    }
    virtual void onTest(const string& message) {

    }
};

class Test : public Event {
public:
    Test() : Event("test") {

    }

    void onTest(const string &message) override {
        cout << "Log message: " + message << endl;
    }
};

int main() {
    vector<Event> t;
    t.push_back(Test());
    t[0].onTest("testMsg");
    return 0;
}

Answer:

The onConsoleMessage method must be virtual:

 virtual void Event::onConsoleMessage(const string& message);

and overridden in ConsoleMessageEvent

 virtual void Event::onConsoleMessage(const string& message) override;

Then the derived class method will be used.

C++ uses std::list std::map std::vector .

Scroll to Top