Pat*_*ryk 5 c++ templates types class
我正在创建一个小的"通用"路径查找类,它采用一种类型Board
,它将在其上找到路径,
//T - Board class type
template<class T>
class PathFinder
{...}
Run Code Online (Sandbox Code Playgroud)
而且Board
还模仿保存节点类型.(这样我就可以在2D或3D矢量空间上找到路径).
我希望能够声明和定义一个成员函数PathFinder
,它将采用这样的参数
//T - Board class type
PathFinder<T>::getPath( nodeType from, nodeType to);
Run Code Online (Sandbox Code Playgroud)
如何为节点类型执行类型兼容性T
并将nodeType
其作为参数提供给函数?
如果我理解你想要什么,给board
一个类型成员并使用它:
template<class nodeType>
class board {
public:
typedef nodeType node_type;
// ...
};
PathFinder<T>::getPath(typename T::node_type from, typename T::node_type to);
Run Code Online (Sandbox Code Playgroud)
如果你不能改变,你也可以模式匹配它board
:
template<class Board>
struct get_node_type;
template<class T>
struct get_node_type<board<T> > {
typedef T type;
};
PathFinder<T>::getPath(typename get_node_type<T>::type from, typename get_node_type<T>::type to);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1401 次 |
最近记录: |