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.
As both Neil Butterworth and Luc Danton noted, you can't have a pointer of type Node* because Node is not a type. It is a template. Yes, Node is a class template, but that class here just qualifies the kind of template. One way to look at it: Just as classes and instances of classes are very different things, so too are templates and template instantiations. It is those template instantiations such as Node that are classes. The class template is some other kind of beast.
There are lots of things you're allowed to do with a pointer, just to list a very few:
For the compiler to generate code to do these efficiently, it needs to know the type of the pointer. If it defered the decisions until it saw the type of the pointer, then it would need to either:
Kind of... you have many options:
void*
, but before it can meaningfully operate on the pointed-to-type again you'll need to manually cast it back to that type: in your case that means recording somewhere what it was then having separate code for every possibilityboost::any<>
- pretty much like avoid*
, but with safety built inboost::variant<>
- much safer and more convenient, but you have to list the possible pointed-to types when you create the pointerNode
that declares the shared functions and member data you'd use to operate on any particular type of node, then the templatedNode
class derives from that abstractNode
and implements type-specific versions of the functions. These are then called via a pointer to the base class usingvirtual
functions.The simple answer is because C++ uses (fairly) strict static type checking.
Node<int>
is a completely unrelated type toNode<double>
, and when the compiler seesptr->doSomething()
, it has to know whether to callNode<int>::doSomething()
orNode<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.)Whenever you create any kind of object (including pointers) in C++, the full type of the object must be known. There is no such type in your code as
Node
, so you can't create instances of pointers to it. You need to rethink how you are designing and writing your code.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>
andNode<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: