By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

pointer to a template class

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Gert Van den Eynde wrote:
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
Jul 22 '05 #2

P: n/a
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
Jul 22 '05 #3

P: n/a
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message news:<c9**********@korweta.task.gda.pl>...
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.


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< > ( )
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<void>() creates a temporary classA<void>.
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;
...
};
Jul 22 '05 #4

P: n/a
Michiel Salters wrote:
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message news:<c9**********@korweta.task.gda.pl>...
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.

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
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.