I am creating a small 'generic' pathfinding class which takes a class type of Board
on which it will be finding paths,
//T - Board class type
template<class T>
class PathFinder
{...}
Whereas Board
is also templated to hold the node type. (so that i can find paths on 2D or 3D vector spaces).
I would like to be able to declare and define a member function for PathFinder
that will take parameters like so
//T - Board class type
PathFinder<T>::getPath( nodeType from, nodeType to);
How can I perform the type compatibility for the node type of T
and nodeType
that is fed into the function as parameter ?
What is Templates in C++? Templates in C++ is an interesting feature that is used for generic programming and templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Simply put, you can create a single function or single class to work with different data types using templates.
A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.
The Template Parameters tool is available for viewing template parameter usage in articles. It works with TemplateData to show the frequency of parameter name usage in a template's mainspace transclusions, along with whether or not each parameter is listed in that template's TemplateData code as a supported parameter.
In UML models, template parameters are formal parameters that once bound to actual values, called template arguments, make templates usable model elements. You can use template parameters to create general definitions of particular types of template.
If I understand what you want, give board
a type member and use that:
template<class nodeType>
class board {
public:
typedef nodeType node_type;
// ...
};
PathFinder<T>::getPath(typename T::node_type from, typename T::node_type to);
You can also pattern match it if you can't change 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);
You can typedef
nodeType
inside class definition:
typedef typename T::nodeType TNode;
PathFinder<T>::getPath( TNode from, TNode to);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With