templates for pointers

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

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
 
L

Larry I Smith

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

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

Regards,
Larry
 
L

Larry I Smith

Abecedarian said:
Because he doesn't want to std::list!

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

Picky, picky, picky :)

Regards,
Larry
 
A

Alvin

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

Sounds like a job for polymorphism.
 
A

Abecedarian

Christian said:
template <class T> class Element
template parameter is any type (that supplies certain functions)
template <class T*> class Element
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::
 
A

Axter

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top