I am looking into working code from Game AI by example book and there is a part I do not understand.
There is
template <class node_type, class edge_type>
class SparseGraph
{ ... };
and
int SparseGraph<node_type, edge_type>::AddNode(node_type node)
{
if (node.Index() < (int)m_Nodes.size())
...
}
How can be node.Index()called?
There also is class
class GraphNode
{
public:
...
int Index()const{return m_iIndex;}
....
};
and graph is created with this class
typedef SparseGraph<GraphNode, GraphEdge> NavGraph;
NavGraph * m_pGraph;
so I understand what node.Index() does, BUT
how can I call node.Index() while there is no guarantee that node_type is GraphNode.
what if node_type is not GraphNode??
Hope you understand my question.
If
node_typeis not GraphNode, then your compiler will smack you and throw an error. However, if your class depends on theIndexfunction, then you should document it as a requirement and any replacement for GraphNode must provide it, probably with some expected semantics.