In .NET I really liked System.Generic.List and wanted to replicate it as a way of learning the C++ language. The problem is that I want it to be generic, I want the elements to contain a value of a template type T. I know most of what to do, but the problem lies within the node.
template<class T>
struct node
{
T value;
bool isFirst;
node *next;
};
I have tried googling this for a while and haven’t found anything relevant. I have just gone through the tutorials at http://www.cplusplus.com. Do I have to reference the class here with a pointer, if so, how?
Thanks in advance
Gisle Aune
There’s nothing wrong with your code. Actually, I would probably write it as
because it’s not much more verbose and it’s clearer that
nextpoints to anodeof the same type, but nevertheless also your version is fine: in a template, when you use the name of the class without explicitly specifying the template parameters, it’s implied that they are the same ones of the template being currently instantiated. This is explained at §14.6.1 of the C++ standard:By the way, writing a generic list is a useful exercise to learn the language, but keep in mind that many generic containers are already available in the C++ standard library; besides the fact that they are throughly tested and optimized, their “iterators” abstraction allow you to use on almost any container the freestanding algorithms available in the library.