473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

pointer to a template class

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
4 6354
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Asfand Yar Qazi | last post by:
Hello. Partly for learning purposes, I have written a smart pointer class. Could you please tell me what's wrong with it? (I know there's something wrong with it, but just not what!) Note...
5
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function? eg, template<class T> fooFunc() {...} fptr = fooFunc; // <-- I couldn't find any info here, not even in the forums:...
4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
15
by: ajj | last post by:
Hello All, Yes this is homework, but I have spent a lot of time on it and I am close. I want to be able to count the number of nodes in a tree that have only one child. I can identify the...
3
by: Nindi73 | last post by:
Hi, I am in need of a deep copy smart pointer (Boost doesn't provide one) which doesnt require the contained types to have a virtual copy constructor. I wrote a smart pointer class that I think...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.