Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template <class = void*>, would appreciate an explanation [closed]

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!

like image 628
Beetroot Avatar asked Dec 30 '25 09:12

Beetroot


1 Answers

= void* is a default template argument. I.e., if you do not specify a type when instanciating the template void* is used. NavGraphNode<> n; will instanciate the template using void* 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.

like image 188
gexicide Avatar answered Jan 01 '26 22:01

gexicide