Preprocessor commandsTopTranslating Java classes into C++Templates

Templates

The syntax for templates in C++ is similar to Java, but they are implemented quite differently. In particular, templates are not type-checked when they are compiled. They only get type-checked when they are instantiated because it is not possible to put constraints on the type parameters. In particular, when they are instantiated with a real type parameter, they are macro-expanded and then compiled with the actual type parameter replacing the formal parameter.

The form of a templated class is:

template <typename T>
class MyClass {
   ...
}

where the type variable T can be used as a type within the class. See Node.h.

Because templates are only compiled when expanded, we normally put the implementation of template classes in the ".h" file with the specification. The same is true of the class stack. (In reality, you would use the stack class in the Standard Template Library (STL), but I thought it would be useful to see a linked list implementation here).


Preprocessor commandsTopTranslating Java classes into C++Templates