Re: Template question
Jeroen wrote:
I have a question which is illustrated by the following piece of code:
template <class T>
class A {
T my_value;
};
In a list,
What's the definition of your 'list'?
I'd like to store pointers to objects of class A.
There is no "class A" until you define what the template argument is.
There is only "class template A".
But I
don't know at forehand if these will be A<int> or A<double> or
whatever (all these flavours of A must be stored in the same list).
There is no such thing as "flavour" in C++. Here you talk about what
is known as "instantiations" of the same template. They are _different_
*types*. Objects of different types cannot be stored in the same
container (unless you define your container in some very tricky and
special way). So, what's the definition of your 'list'?
And, lateron if I retrieve the objects from the list I may have to
know what flavour the object was in order to call the proper routines
for processing them.
My question: what does the list look like in C++? I'm lost, so if
anyone has some advice....
What problem are you trying to solve? It is very likely that you
need a "heterogeneous container". Look it up. But it's impossible
to advise anything given such vague "problem definition".
Also read the FAQ. What you seem to describe here is usually solved
through polymorphism (and it's not all that difficult). But you did
not say what kind of "processing" you are going to do, nor does your
class template 'A' have any functionality, which begs the question,
why don't you just store 'ints' and 'doubles' in your 'list'?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask`