Connecting Tech Pros Worldwide Help | Site Map

templates for pointers

  #1  
Old July 23rd, 2005, 04:57 AM
Christian Christmann
Guest
 
Posts: n/a
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





  #2  
Old July 23rd, 2005, 04:58 AM
Larry I Smith
Guest
 
Posts: n/a

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.
  #3  
Old July 23rd, 2005, 04:58 AM
Abecedarian
Guest
 
Posts: n/a

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*> ?

  #4  
Old July 23rd, 2005, 04:58 AM
Larry I Smith
Guest
 
Posts: n/a

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.
  #5  
Old July 23rd, 2005, 04:58 AM
Alvin
Guest
 
Posts: n/a

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.

  #6  
Old July 23rd, 2005, 04:58 AM
Abecedarian
Guest
 
Posts: n/a

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::

  #7  
Old July 23rd, 2005, 04:58 AM
Axter
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Templates for syntatic niceness only, does it waste space? collection60@hotmail.com answers 8 July 24th, 2006 04:15 PM
sorting list of pointers Nenad Jalsovec answers 13 July 22nd, 2005 06:56 PM
Templates, syntax and allocation! Mike answers 6 July 22nd, 2005 12:58 PM
Template specialization of pointers with function pointers Phil answers 1 July 19th, 2005 06:35 PM