Hamaro
Hamaro

Reputation: 49

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:

};

Upvotes: 0

Views: 2081

Answers (1)

user7860670
user7860670

Reputation: 37588

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;
 }

Upvotes: 6

Related Questions