pointer to a template class

  • Thread starter Gert Van den Eynde
  • Start date
G

Gert Van den Eynde

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
 
V

Victor Bazarov

Gert said:
A beginners question....

I've got a template class
Nope.


template <class T> classA {...}

You have a _class_template_. That's not the same thing.
In an other class, I want to pass a pointer to an instance of classA

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_.
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?

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
 
M

Marcin Kalicinski

Hi,
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?

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
 
M

Michiel Salters

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;
...
};
 
V

Victor Bazarov

Michiel said:
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< > ( )

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.
[...]
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;
...
};

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top