When to use pointer template parameters?
I created a templated class which searches for a path between 2 points in a graph. The template parameter takes in a class representing the graph.
template <class graph_type>
class Graph_PathSearch
{
public:
Graph_PathSearch(&graph_type G, int startIndex, int endIndex);
};
The graph object is declared as a pointer.
NavGraph* m_pGraph;
m_pGrapn = new NavGraph(true);
I then instantiated the path search using this statement:
Graph_PathSearch<NavGraph*> StartSearch(*m_pGraph,startNode,destNode);
But the compiler produced a long string of compile errors. However, when I changed the template parameter from <NavGraph*> to <NavGraph>, there were no more compile errors.
I would like to know when is the right time to use a pointer as a template parameter, since the graph is declared as a pointer but using a pointer as a template parameter is incorrect in this case?
Re: When to use pointer template parameters?
Quote:
Originally Posted by
ZhiYi
I then instantiated the path search using this statement:
Graph_PathSearch<NavGraph*> StartSearch(*m_pGraph,startNode,destNode);
But the compiler produced a long string of compile errors. However, when I changed the template parameter from <NavGraph*> to <NavGraph>, there were no more compile errors.
Maybe the errors were legitimate. It's impossible to know if you don't post your entire code.
Second, if the pointer is typedef'ed, it may resolve some of the errors:
Code:
typedef NavGraph* NavGraphPtr;
And then use NavGraphPtr.
Regards,
Paul McKenzie
Re: When to use pointer template parameters?
Quote:
But the compiler produced a long string of compile errors. However, when I changed the template parameter from <NavGraph*> to <NavGraph>, there were no more compile errors.
Based on the code you posted, you call the function with NavGraph* as the template parameter, but in first argument to that function you dereference the pointer m_pGraph, hence you're actually passing a NavGraph object to the function.