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 4 6379
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_argum ent_.
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_typ e_. 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_cl assA_of_int;
classA<double>* a_pointer_to_an _object_of_clas sA_of_double;
Victor
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<typena me T> void SomeFunction(cl assA<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
"Marcin Kalicinski" <ka****@poczta. onet.pl> wrote in message news:<c9******* ***@korweta.tas k.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<typena me T> void SomeFunction(cl assA<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<in t> 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<typena me 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_nod e {
DATA d; // <=== data member
linked_list_nod e* next, prev;
...
};
Michiel Salters wrote: "Marcin Kalicinski" <ka****@poczta. onet.pl> wrote in message news:<c9******* ***@korweta.tas k.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_nod e { DATA d; // <=== data member linked_list_nod e* next, prev; ... };
He meant _independent_ member templates that are data members:
template<typena me T>
class whatever {
template<class S> double A; // what's that going to do?
};
You can have member templates for functions:
template<typena me T>
class whatever {
template<class S> double foo(); // 'foo' is a func template
};
Victor This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 that, as well as reference counting the stored pointer, it also has
the ability to be declared for incomplete types (I think). I.e.:
struct...
|
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:
http://www.function-pointer.org/
|
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 classes
|
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 nodes with the following code, but I don't know how
to sum them up and return that value to the calling function in main().
I know the algorithm is...
|
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 meets these
requirements, but after reading the chapter on exceptions in
'Exceptional C++':Sutter, I am not sure if its is really Exception safe
or...
| |
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 exception
neutral/
I got a reply from Bux with his code for a smart pointer with far fewer
lines of code
and more cleaner design, not over engineered...
|
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 to functions are hard to
inline for the compiler, and so you won't be able to avoid function
call overhead.
This is an important aspect when you are...
|
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 implementation so the two can vary independently.
Handle classes usually contain a pointer to the object implementation. The Handle object is used...
|
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):
//------------------------------------------------------------------
template<typename Data_t>
class SmartPointer
{
Data_t* data;
void(*deleterFunc)(Data_t*);
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |