mja*_*mes 2 c++ architecture templates composition
我有一个基于"实体"的程序,它包含"组件"(组成FTW).
组件可能包括许多不同的类型,包括脚本,资产等.我想构建一个名为Entity的函数
实体具有字符串映射和实际组件,以便可以按类型名称搜索组件.
我想要一个叫做的函数
<Component T>GetComponent(char* TypeName, <T>);
Run Code Online (Sandbox Code Playgroud)
其中包含字符串和类型名称,并返回所请求的类型化组件.
用C++模板做这样的事情有可能吗?以上显然不起作用,我不知道如何去做.
谢谢
编辑:
我不是在找工厂.
实体保存不同类型组件的实例.目前这已经完成了
std::vector<Component> componentList;
Run Code Online (Sandbox Code Playgroud)
和
std::vector<char*> componentNames;
Run Code Online (Sandbox Code Playgroud)
谁的索引保证是相同的.我可能稍后会写一张合适的地图.
我只是希望GetComponent返回一个正确类型的引用,该引用指向ComponentList中Entity持有的类型名称的已定时组件.
sbi*_*sbi 11
你的功能是否创造了组件?然后是工厂.您可以将其包装在该模板中,以便为客户端保存(可能是错误的)转换.
函数模板的类型如下所示:
template< typename T >
T* GetComponent(const char*); // presuming it returns a pointer
Run Code Online (Sandbox Code Playgroud)
它将被称为这样:
Foo* foo = GetComponent<Foo>("foo");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
879 次 |
| 最近记录: |