クラスのメンバとして存在するstd::map<size_t, std::shared_ptr<Component>> にTemplateな型を挿入・取得するクラスを作ろうとしています。
ManagerクラスのAdd()挿入はうまく動くのですが、Get()がコンパイルエラーとなり実行できませんどなたかお力をお貸しください
エラーメッセージ
C2440 'return': '_Ty2' から 'T *' に変換できません。
<```C++20
コード
#include <map>
#include<typeinfo>
#include<memory>
#include<iostream>
using namespace std;
class Component
{
public:
};
class Derived :public Component
{
public:
int mData{};
};
class Manager
{
public:
// 要素を追加 template<class T> T* Add() { auto s = typeid(T).hash_code(); cout << s << endl; auto t = new T(); mMap.try_emplace(s,t); return t; } // 要素を取得 template<class T> T* Get() { const auto s = typeid(T).hash_code(); auto a = mMap.find(s); if(a!=mMap.end()) { return a->second; } return nullptr; }
private:
std::map<size_t, std::shared_ptr<Component>> mMap{};
};
void main()
{
Manager manager;
auto derived1=manager.Add<Derived>(); // 実行できる
auto derived = manager.Get<Derived>(); // 実行できない`
}

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。