473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

Jul 23 '05 #1
6 1331
Christian Christmann wrote:
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

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #2
Larry I Smith wrote:

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


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

Jul 23 '05 #3
Abecedarian wrote:
Larry I Smith wrote:
Why not just use the 'std::list'?
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

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #4
Christian Christmann wrote:
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.

Jul 23 '05 #5
Christian Christmann wrote:
template <class T> class Element template parameter is any type (that supplies certain functions)
or 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::

Jul 23 '05 #6

Christian Christmann wrote:
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.

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mike | last post by:
Hey! I've started to use templates for storing arrays of doubles. The template itself will then be passed on to a linked-list. My problem is that Im not 100% familiar with how templates are...
3
by: Filipe Valepereiro | last post by:
I all. I need to write a function that convert one string into a vector. This string represent a serialized form of the vector. So i come up with this piece of code, that compile just fine....
7
by: Robert Bralic | last post by:
Dear, I wreated a small program that make a linked list of factorials of numbers, I don't have expirience with templates so I will bee thankfull if anybody can make the same with...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
12
by: Ben | last post by:
I'm kind of new to creating templates. I've made some small class and function templates in the past and I have used quite of bit of the STL, but I am having problems tyring to create templates. ...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
3
by: Ernesto Bascón | last post by:
Hi everybody: I have two questions: 1. I'm using opaque pointers in my classes to hide their data structures; there is a way to use opaque pointers in template classes; since the...
2
by: Pat | last post by:
Hello everyone, I have a question that even my instructor is perplexed. I have created a template class within a header file. Then in a cpp file I have the methods that go with the class. I have...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: Chris | last post by:
Hi All, This is a weird one but I am hoping someone can help or has some pointers, a recipe how to do the following: I have to move some code from c++ to objective-c and to do this I must...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.