Marcin Kalicinski said:
Hi,
Instance of classA is a class (because classA is a template), so you cannot
pass it to some function or store it in a variable. Classes cannot be stored
or passed to functions. You can only do that with classA<X> where X is some
type name. Instance of classA<X> is an object, and you can store or pass it
freely.
Actually, you can pass a class to a function _template_, so you could
pass a class template instance as well:
template< class C > void foo();
template< class T > classA {};
foo said:
Alternatively, you could define the function you want to pass an instance of
classA as a template function:
template<typename T> void SomeFunction(classA<T> p);
Compiler will then automatically create separate version of SomeFunction for
every type T you use it with.
True, if you have instances of instances of classA
, e.g an instance of
classA<int>, you can pass that and the compiler will generate
SomeFunction<int> to handle the classA<int>. But sometimes you can't do
that, because you don't have the object yet. In such cases, you pass
the class and get an instance of class:
template<typename T>
classA<T> create() { return classA<T>( ); }
create said:
You cannot do that with data member though,
because there are no data members templates in C++.
I'm not sure what that means. C++ has only class templates and
function templates, but class templates certainly can have
data members whose types are dictated by the surrounding template.
template< typename DATA >
class linked_list_node {
DATA d; // <=== data member
linked_list_node* next, prev;
...
};