Connecting Tech Pros Worldwide Forums | Help | Site Map

templates for pointers

Christian Christmann
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi,

I need a template list where I can store pointers to
different objects. The list consists of linked objects
of the class Element which contain the pointer information.

How do I define the template class Element in the header file?

template <class T> class Element
{
T* info;
...
}

or
template <class T*> class Element
{
T* info;
...
}

And let's say I want to store pointers to an object Node.
What is the right initialization in the source code:

....
Node *newNode;
Element<Node> = new Element<Node>

or

Node *newNode;
Element<Node*> = new Element<Node*> ?


Thanks
Chris






Larry I Smith
Guest
 
Posts: n/a
#2: Jul 23 '05

re: templates for pointers


Christian Christmann wrote:[color=blue]
> Hi,
>
> I need a template list where I can store pointers to
> different objects. The list consists of linked objects
> of the class Element which contain the pointer information.
>
> How do I define the template class Element in the header file?
>
> template <class T> class Element
> {
> T* info;
> ...
> }
>
> or
> template <class T*> class Element
> {
> T* info;
> ...
> }
>
> And let's say I want to store pointers to an object Node.
> What is the right initialization in the source code:
>
> ...
> Node *newNode;
> Element<Node> = new Element<Node>
>
> or
>
> Node *newNode;
> Element<Node*> = new Element<Node*> ?
>
>
> Thanks
> Chris
>
>
>
>
>[/color]

Why not just use the 'std::list'?

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Abecedarian
Guest
 
Posts: n/a
#3: Jul 23 '05

re: templates for pointers


Larry I Smith wrote:[color=blue]
>
> Why not just use the 'std::list'?[/color]

Because he doesn't want to std::list!
BTW, do you mean std::list<T> or std::list<T*> ?

Larry I Smith
Guest
 
Posts: n/a
#4: Jul 23 '05

re: templates for pointers


Abecedarian wrote:[color=blue]
> Larry I Smith wrote:[color=green]
>>Why not just use the 'std::list'?[/color]
>
> Because he doesn't want to std::list![/color]

Hmm, he didn't say that. If he is
new to C++, he may not know about the STL.
[color=blue]
> BTW, do you mean std::list<T> or std::list<T*> ?
>[/color]

Picky, picky, picky (:

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Alvin
Guest
 
Posts: n/a
#5: Jul 23 '05

re: templates for pointers


Christian Christmann wrote:
[color=blue]
> Hi,
>
> I need a template list where I can store pointers to
> different objects. The list consists of linked objects
> of the class Element which contain the pointer information.
>
> How do I define the template class Element in the header file?
>
> template <class T> class Element
> {
> T* info;
> ...
> }
>
> or
> template <class T*> class Element
> {
> T* info;
> ...
> }
>
> And let's say I want to store pointers to an object Node.
> What is the right initialization in the source code:
>
> ...
> Node *newNode;
> Element<Node> = new Element<Node>
>
> or
>
> Node *newNode;
> Element<Node*> = new Element<Node*> ?
>
>
> Thanks
> Chris[/color]

Sounds like a job for polymorphism.

Abecedarian
Guest
 
Posts: n/a
#6: Jul 23 '05

re: templates for pointers


Christian Christmann wrote:[color=blue]
> template <class T> class Element[/color]
template parameter is any type (that supplies certain functions)
[color=blue]
> or[/color]
[color=blue]
> template <class T*> class Element[/color]
template parameter is a pointer to T

In general, the first solution is more flexible. You can e.g write:
template <class T> class Element
{
T* info;
T x;
};

OTOH,
template <class T*> class Element
{
T* info; // ???
};

.... is hardly what you want (try it with your compiler).


::A::

Axter
Guest
 
Posts: n/a
#7: Jul 23 '05

re: templates for pointers



Christian Christmann wrote:[color=blue]
> Hi,
>
> I need a template list where I can store pointers to
> different objects. The list consists of linked objects
> of the class Element which contain the pointer information.
>
> How do I define the template class Element in the header file?
>
> template <class T> class Element
> {
> T* info;
> ...
> }
>
> or
> template <class T*> class Element
> {
> T* info;
> ...
> }
>
> And let's say I want to store pointers to an object Node.
> What is the right initialization in the source code:
>
> ...
> Node *newNode;
> Element<Node> = new Element<Node>
>
> or
>
> Node *newNode;
> Element<Node*> = new Element<Node*> ?
>
>
> Thanks
> Chris[/color]

Check out the code examples for a Heterogeneous Container in the
following links:
http://code.axter.com/HeterogeneousContainer.cpp
http://code.axter.com/HeterogeneousContainer_2.cpp

The above Heterogeneous Container can hold any type, as long as all the
types have some function or inteface in common.

Closed Thread