Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template class pointer c++ declaration

Tags:

template <typename T> class Node {...};  int main {     Node* ptr;     ptr = new Node<int>; } 

Will fail to compile I have to to declare the pointer as

Node<int>* ptr; 

Why do I have to specify the type when declaring a pointer I haven't created the class yet, why does the compiler have to know what type it will be pointing to. And is it not possible to create a generic pointer and decide afterwards what type I want to assign it.

like image 553
randomBananas Avatar asked Jun 14 '11 10:06

randomBananas


People also ask

How do you declare a pointer to a class?

A pointer to a C++ class is done exactly the same way as a pointer to a structure and to access members of a pointer to a class you use the member access operator -> operator, just as you do with pointers to structures. Also as with all pointers, you must initialize the pointer before using it.

How do you declare a template class?

To instantiate a template class explicitly, follow the template keyword by a declaration (not definition) for the class, with the class identifier followed by the template arguments. template class Array<char>; template class String<19>; When you explicitly instantiate a class, all of its members are also instantiated.

Can template type be a pointer?

A template has only one type, but a specialization is needed for pointer, reference, pointer to member, or function pointer types. The specialization itself is still a template on the type pointed to or referenced.

How do you define a templete class?

Definition. As per the standard definition, a template class in C++ is a class that allows the programmer to operate with generic data types. This allows the class to be used on many different data types as per the requirements without the need of being re-written for each type.


2 Answers

Templating resolves types at compile-time. When you assign the new Node<int> object to it, the pointer must know at compile-time what type exactly it is.

Node<int> and Node<std::vector> can be very different in the binaries (the binary layout of your object changes completely according the template parameter) so it doesn't make any sense to have an unresolved pointer type to a template.

You should define first a common parent class for your nodes:

class NodeBase { ... }  template<typename ValueT>   class Node : public NodeBase {  ... };  NodeBase* ptr; 
like image 85
progician Avatar answered Oct 07 '22 13:10

progician


The simple answer is because C++ uses (fairly) strict static type checking. Node<int> is a completely unrelated type to Node<double>, and when the compiler sees ptr->doSomething(), it has to know whether to call Node<int>::doSomething() or Node<double>::doSomething().

If you do need some sort of dynamic generality, where the actual type ptr will point to will only be known at runtime, you need to define a base class, and derive from that. (It's a fairly common idiom for a class template to derive from a non-template base, precisely so that the generality in the pointers can be resolved at runtime.)

like image 27
James Kanze Avatar answered Oct 07 '22 11:10

James Kanze