| re: pointer to a template class
"Marcin Kalicinski" <kalita@poczta.onet.pl> wrote in message news:<c9ncqc$bme$1@korweta.task.gda.pl>...[color=blue]
> Hi,
>[color=green]
> > I've got a template class
> >
> > template <class T> classA {...}
> >
> > In an other class, I want to pass a pointer to an instance of classA as a
> > function argument. How do I do this? Also, I want to have a data member[/color]
> for[color=green]
> > this pointer. How do I declare this datamember?[/color]
>
> 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.[/color]
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< classA<int> > (); // passes classA<int> to foo< > ( )
[color=blue]
> 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.[/color]
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<void>() creates a temporary classA<void>.
[color=blue]
> You cannot do that with data member though,
> because there are no data members templates in C++.[/color]
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;
...
}; |