sesodesa
sesodesa

Reputation: 1603

Why isn't my implementation of a virtual function being called in this case?

I have access to an interface, that implements the virtual method iObjectManager::getTile(const Course::ObjectId &id) = 0. I've reimplemented said method in an inherited class ObjectManager:public Course::iObjectManager as follows:

std::shared_ptr<Course::TileBase> getTile(const Course::ObjectId &id) override;

std::shared_ptr<Course::TileBase> ObjectManager::getTile(const Course::ObjectId &id)
{
  qDebug() << "Looking for tile based on ID...\n";
  for (unsigned i = 0; i < getMapSize(); i++) {
    for (unsigned j = 0; j < getMapSize(); j++) {
      std::shared_ptr<BoardPiece> tile = getGameMap().at(i).at(j);
      if (tile->ID == id) {
        qDebug() << "Tile found.\n";
        return std::move(tile);
      }
    }
  }
  qDebug() << "No tile with given index...\n";
  return nullptr;
}

The issue arises, when the course side code calls getTile: nothing happens. The calls to qDebug are not made and nothing is printed. Why would the function fail to get called (or the one lacking an implementation get called) even though I've implemented the function properly (I think)?

The functio is called as follows:

tile = lockObjectManager()->getTile(ID);

where lockObjectManager is defined as

std::shared_ptr<iObjectManager> GameObject::lockObjectManager() const
{
    std::shared_ptr<iObjectManager> handler = OBJECTMANAGER.lock();
    Q_ASSERT(handler);
    return handler;
}

Edit

I've just discovered, that my ObjectManager is destroyed right after its initialization. This is weird, as I have it set as a member of my MainWindow right at the start of its construction, but it is also given to my GameEventHandler as an initialization parameter, and the gameEventHandler does not die either (based on my qDebugging):

GOManager_ = std::make_shared<ObjectManager>(playerAges_);
GEHandler_ = std::make_shared<GameEventHandler>(GOManager_);

Either the main window of the eventhandler (or both) should be keeping the objectmanager alive, but they are not... Is this because I've not provided an assignment operator, as described here?

Upvotes: 0

Views: 86

Answers (1)

Saisai3396
Saisai3396

Reputation: 321

Make sure OBJECTMANAGER.lock() is returning an instance of the derived ObjectManager class. If it is returning iObjectManager(), it will call its base version of getTile(), not the overloaded one.

Upvotes: 1

Related Questions