Search code examples
c++polymorphismgame-enginedynamic-cast

Cannot use dynamic_cast to convert from Base to Derived


I get an error when I try to cast base class to derived class. I want to access the derived classes that I have put in my components vector.

//Base and Derived

class Component
{
public:
    Component();
    virtual ~Component();
private:
};

class Material:public Component{...};

//in main

int textureID = gameScene.gameObjects[0].getComponent<Material>()->texture;

//game object

#pragma once
#include <vector>
#include "Component.h"

class GameObject:public Component
{
public:
    GameObject();
    GameObject(int otherAssetID);
    ~GameObject();

    int assetID;
    std::vector<Component> components;

    void addComponent(Component otherComponent);
    void deleteComponent(Component otherComponent);

    template <class T>
    T* getComponent() {
        for (int i = 0; i < components.size(); i++)
        {
            if (dynamic_cast<T*>(components[i]) != nullptr)
            {
                T *test = dynamic_cast<T*>(components[i]);
                return test;
            }
        }

        return nullptr;
    }
private:

};

Solution

  • std::vector<Component> can not contain objects of class other than Component itself. If you add a Material object to the vector, the Component part of the Material will be stored. That problem is known as the object slicing problem.

    You probably want to make a vector holding pointers to the base polymorphic class.

    ::std::vector<::std::unique_ptr<Componenet>> components;
    

    also dynamic_cast is expensive so you may want to call it only once storing returned value:

     T * const test = dynamic_cast<T*>(components[i].get());
     if(test)
     {
         return test;
     }