-4
まずは、類似のアーキテクチャを持つゲームエンジンをUnityに作成しようとしています。私はObjectクラスを持っています。Objectクラスから継承したGameObjectクラスです。 GameObjectクラス内で、私はゲームオブジェクトに任意のコンポーネントを追加できるテンプレートクラスを作成しようとしていますが、私は解決できないような動的ポインタキャストエラーが発生します。GAME ENGINE:dynamic_cast - オブジェクトクラスは多態型ではありません
相続人はゲームオブジェクトクラス:
class GameObject : public Object
{
public:
template<class T>
std::shared_ptr<T> addChildComponent()
{
std::shared_ptr<T> temp_component(new T());
temp_component->gameObject = std::dynamic_pointer_cast<GameObject>(shared_from_this());
m_components.push_back(temp_component);
return temp_component;
}
template<class T>
std::shared_ptr<T> getComponent()
{
for (int i = 0; i < m_components.size(); i++)
{
std::shared_ptr<T> t;
t = m_components.at(i);
if (t.get() != NULL)
{
return t;
}
}
return std::shared_ptr<T>();
}
さらに、これはそれから継承するオブジェクトクラスです:
class Object : public std::enable_shared_from_this<Object>
{
friend class GameObject;
public:
Object();
~Object();
void makeName(std::string _name);
std::string getName();
private:
std::string name;
};