Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Apr 3, 1998

Strange linked list

Question:
How can I create a linked list that contains different datatypes? For example: assume that I have Human as a basic class. Man and Woman are two children to this class. How can I make a linked list that contains 10 men and 15 women?

Answer:
The easiest way is probably to place a pointer to your base class in each node of the linked list. This way, the pointer and nodes will always be the sames size, and only the data the pointer points to changes. Note that this approach requires you to allocate the memory to hold the class object as well.

Another approach you can take is to create a base class called CNode that has a next and, optionally, a previous pointer. You can then derive your Human class from CNode. This allows you to treat each item in the list as a CNode, even though each item will contain additional information.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date