2012-11-20 153 views
4

我創建一個小的「通用」尋路類,它需要一個類類型的Board上,它會尋找路徑,獲取模板,模板類型

//T - Board class type 
template<class T> 
class PathFinder 
{...} 

Board也模板持有節點類型。 (這樣我就可以在2D或3D矢量空間上找到路徑)。

我想能夠聲明和定義成員函數爲PathFinder,將帶參數,像這樣

//T - Board class type 
PathFinder<T>::getPath(nodeType from, nodeType to); 

如何可以執行的T節點類型的類型兼容性和nodeType被饋送進入函數作爲參數?

+0

nodeType和T是一樣的嗎? –

回答

6

如果我知道你想什麼,給board類型成員和使用:

template<class nodeType> 
class board { 
    public: 
    typedef nodeType node_type; 
    // ... 
}; 

PathFinder<T>::getPath(typename T::node_type from, typename T::node_type to); 

你也可以模式匹配,如果你無法更改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); 
+0

這是幹什麼用的? '模板 struct get_node_type;' –

+0

@KarthikT沒有這些,你就不能部分專精。 – Pubby

2

可以typedefnodeType內部類的定義:

typedef typename T::nodeType TNode; 
PathFinder<T>::getPath(TNode from, TNode to);