Templets

Started by
8 comments, last by ATronic 22 years, 4 months ago
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
Alex BroadwinA-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!"
Advertisement

A templet most certainly won''t work.
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.
Alex BroadwinA-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!"
Maybe you should try a template instead.
Yes, yes. I can''t spell. You aren''t very helpful now are you? Anyone with some _useful_ information?
Alex BroadwinA-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!"
The last version should work.
Thanks a ton.
Alex BroadwinA-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!"
Jeez everyone is a comedian...

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

MemoryBlock&ltint> *var = new MemoryBlock&ltint>

and:

MemoryBlock&ltint> *var = new MemoryBlock&ltint>[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
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
I hear that! Thanks again.
Alex BroadwinA-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!"
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
Edem Attiogbe

This topic is closed to new replies.

Advertisement