Connecting Tech Pros Worldwide Forums | Help | Site Map

pointer to a template class

Gert Van den Eynde
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi all,

A beginners question....

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 for
this pointer. How do I declare this datamember?


thanks,
gert

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: pointer to a template class


Gert Van den Eynde wrote:
[color=blue]
> A beginners question....
>
> I've got a template class[/color]

Nope.
[color=blue]
>
> template <class T> classA {...}[/color]

You have a _class_template_. That's not the same thing.
[color=blue]
> In an other class, I want to pass a pointer to an instance of classA[/color]

There is no such thing as "instance of classA" (no matter how
many times you use the word 'class' in its name, it's still just
a _template_). In order to have an _instance_ you need to make
an _instantiation_ of the 'classA' _template_. To do that you
need to give it a valid _template_argument_.
[color=blue]
> as a
> function argument. How do I do this? Also, I want to have a data member for
> this pointer. How do I declare this datamember?[/color]

Again, there is no such thing as "a pointer to a template". You
can only have a pointer to an object of a _particular_type_. In
order to _become_ a particular type, a template needs to be
_instantiated_, and that means you need to give it the set of
valid arguments (in your case, one argument that is in turn also
a type).

Example:

classA<int> an_object_of_classA_of_int;
classA<double>* a_pointer_to_an_object_of_classA_of_double;

Victor
Marcin Kalicinski
Guest
 
Posts: n/a
#3: Jul 22 '05

re: pointer to a template class


Hi,
[color=blue]
> 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=blue]
> 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.

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. You cannot do that with data member though,
because there are no data members templates in C++.

Marcin


Michiel Salters
Guest
 
Posts: n/a
#4: Jul 22 '05

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;
...
};
Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 22 '05

re: pointer to a template class


Michiel Salters wrote:[color=blue]
> "Marcin Kalicinski" <kalita@poczta.onet.pl> wrote in message news:<c9ncqc$bme$1@korweta.task.gda.pl>...
>[color=green]
>>Hi,
>>
>>[color=darkred]
>>>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=darkred]
>>>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]

You're not passing it to a function. You're giving it to the compiler
to use as a template argument. That's not the same thing.
[color=blue]
> [...]
>[color=green]
>>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;
> ...
> };[/color]

He meant _independent_ member templates that are data members:

template<typename T>
class whatever {
template<class S> double A; // what's that going to do?
};

You can have member templates for functions:

template<typename T>
class whatever {
template<class S> double foo(); // 'foo' is a func template
};

Victor
Closed Thread