Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 23rd, 2005, 04:57 AM
Christian Christmann
Guest
 
Posts: n/a
Default templates for pointers

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
Default 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
Default 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
Default 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
Default 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
Default 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
Default 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.

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles