Archived

This topic is now archived and is closed to further replies.

ATronic

Templets

Recommended Posts

ATronic    122
Hey all, I was working on a linked list dynamic memory class when I realized something. I don't know how to dynamicly allocate templet objects. Such as this. Say I have a templet class MemoryBlock. Normally I would say
  MemoryBlock<int> block;  
right? er, or was it
  MemoryBlock block<int>;  
Anyway, what I want to know is this. If I want to dynamically allocate a templet class, where does the type go? would I do this:
  MemoryBlock<int> *var = new MemoryBlock;  
or
  MemoryBlock *var = new MemoryBlock<int>;  
or
  MemoryBlock<int> *var = new MemoryBlock<int>;  
???? Any help is greatly appreciated. Thanks ahead of time. Alex Broadwin A-Tronic Software & Design ----- "if you fail in life, you were destined to fail. If you suceed in life, call me." "The answer is out there." "Please help, I'm using Windows!" Edited by - ATronic on December 18, 2001 12:45:50 PM

Share this post


Link to post
Share on other sites
ATronic    122
I''m sure it will work. When people do what I am doing, they do not make a class for every data type. And I know people do what I am doing. I just need to know the format. If you have anywhere near a decent compiler, this should work.

Share this post


Link to post
Share on other sites
Dire.Wolf    122
Jeez everyone is a comedian...

To dynamically allocate a templated class object you do so pretty much like anything else dynamic:

MemoryBlock<int> *var = new MemoryBlock<int>

and:

MemoryBlock<int> *var = new MemoryBlock<int>[10]; // array of 10 integer MemoryBlocks.

Hopefully this helps you out.

BTW In C++ the type list for the class comes after the class name, not the variable.




Dire Wolf
www.digitalfiends.com

Edited by - Dire.Wolf on December 18, 2001 1:11:02 PM

Share this post


Link to post
Share on other sites
KwamiMatrix    122
This is a little part of my header file:

template class anything
//This is Edem Kwami Attiogbe's Linked List Managing Class
class KwamiLink


private:
struct Link
{
anything any;
int id;
Link *next;
};

Link *head,*tail;
bool ListEmpty();

I templated the whole class and made a struct.

Edem Attiogbe

Edited by - KwamiMatrix on December 18, 2001 1:26:02 PM

Share this post


Link to post
Share on other sites