473,386 Members | 1,654 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.

Is it possible to use a template-like approach for classes?

18
Hello there. Long time no post. :P Well here goes...

I'm writing a program that supports many data structures (stacks,heaps etc.) made from scratch. However the program does not limit you to just one stack, one heap etc. but you can have as many as you like. I'm looking for a way to store all the different stacks into one singly-linked list and then all the heaps into another singly-linked list etc. So basically I'd like to make a class (for example "class linkedlist") that supports a list that is not dependant on each data structure. It's confusing but maybe you'll get what I mean by code:

Expand|Select|Wrap|Line Numbers
  1. class linkedlist
  2. {
  3. private:
  4.     struct structureIDs
  5.     {
  6.         int ID;
  7.         <<class name goes here>> newStructure;
  8.         struct structureIDs *next;
  9.     }*head;
  10. public:
  11. //...
  12. };
That's the general idea... Where <<class name goes here>> is the name of the class associated to the data structure. So for a list that "keeps" all the stacks you'd have:

Expand|Select|Wrap|Line Numbers
  1. class linkedlist
  2. {
  3. private:
  4.     struct structureIDs
  5.     {
  6.         int ID;
  7.         stack stackno1; //Assuming that the class that creates a stack is named "stack"
  8.         struct structureIDs *next;
  9.     }*head;
  10. public:
  11. //...
  12. };
Whereas for a heap you'd have...

Expand|Select|Wrap|Line Numbers
  1. class linkedlist
  2. {
  3. private:
  4.     struct structureIDs
  5.     {
  6.         int ID;
  7.         heap heapno1; //Assuming that the class that creates a heap is named "heap"
  8.         struct structureIDs *next;
  9.     }*head;
  10. public:
  11. //...
  12. };
Now the problem is that by this method you'd have to completely write new classes for stack-linkedlists, for heap-linkedlists etc. Is there any way to "tell" the compiler to expect a class at that point but not determine its kind? (Something like templates?)...

Really hope you people answer, thanks in advance!
Nov 12 '07 #1
2 1108
Why not use templates itself
Nov 12 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
You might consider a template for the nodes. Maybe:
Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2. class Node
  3. {
  4. private:
  5.     T theData;
  6. };
  7. template <class T>
  8. class LinkedList
  9. {
  10.     private:
  11.     Node<T>* start;
  12.     Node<T>* end;
  13.     Node<T>* current;
  14. };
  15.  
Then you can create a linked list of any type:
Expand|Select|Wrap|Line Numbers
  1. LinkedList<int> IntList;
  2. LinkedList<MyClass> MyClassList;
  3. //etc...
  4.  
Nov 12 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rennie deGraaf | last post by:
The attached code compiles and works properly when I comment out the declaration, definition, and invocations of the method 'eck'. With "eck" in there, g++ fails with ttest.cpp:23: template-id...
2
by: FrankS | last post by:
Hi All, I have a problem with an call-template cmd at xslt 1.0: With: pCall = 'ExInput' ------ I try to: <xsl:call-template name="{$pCall}"> <xsl:with-param name="pVal" select="$pValue"/>...
18
by: skishorev | last post by:
Hi, Here I am taking two functions. void f(int,int) and another one is float f(int,int). Is it possible to overload with return values. Thx, kishore
7
by: quarup | last post by:
I want to specialize a template function that lives inside a class, but am getting a compile error in VS.net 2003. Here's my code: template <class T> class A { public: template <class U> void...
6
by: Peng Yu | last post by:
Hi, I want B has all the constructors that A has. Obviously, the code below would not work. I could define a corresponding B's constructor for each A's constructor. But if A has many...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.