I am reading a book where the guy makes a linked list
he creates a class like this
template < class extra_info = void*>
class NavGraphNode : public GraphNode
{
protected:
//the node's position
Vector2D m_vPosition;
extra_info m_ExtraInfo;
public:
/*INTERFACE OMITTED */
};
He explains extra_info could be for example an enumerated value or a pointer
to the instance the node is twinned with. But I don’t really understand the first line,
reading for example
http://www.cplusplus.com/doc/tutorial/templates/
it seems that if you specify the type (and why not void* extra_info?)
then why use a template in the first place?
Thanks!
= void*is a default template argument. I.e., if you do not specify a type when instanciating the templatevoid*is used.NavGraphNode<> n;will instanciate the template usingvoid*as extra info.However, you CAN explicitly specify a type, then this type is used. E.g., you could use
NavGraphNode<int>to add an integer as extra info to you graph node. You can also use whole structs or pointers to those to add more info to a node.