www.digitalmars.com         C & C++   DMDScript  

D - On the implementation of a generalized Stack class

reply Charles Hixson <charleshixsn earthlink.net> writes:
Would it be more appropriate to implement a stack class as a 
template, or as a stack of objects?  What are the tradeoffs?
Oct 08 2003
parent reply "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> writes:
"Charles Hixson" <charleshixsn earthlink.net> escreveu na mensagem
news:bm1lim$30th$1 digitaldaemon.com...
 Would it be more appropriate to implement a stack class as a
 template, or as a stack of objects?  What are the tradeoffs?

An object Stack isn't type safe, and you need to cast the objects to their correct types. Also, since everything isn't an object, it can't be used with primitives other than object references. If you have a template Stack you can have both ways: module stack; template TStack(T) { class Stack { ... } } alias instance TStack(Object).Stack Stack; The users can use an unsafe stack.Stack or instantiate their own stack.TStack(T).Stack I don't recommend creating such alias, since it endorses an unsafe style of programming Best regards, Daniel Yokomiso. "It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration." - Edsger W. Dijkstra --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.524 / Virus Database: 321 - Release Date: 6/10/2003
Oct 08 2003
parent Charles Hixson <charleshixsn earthlink.net> writes:
Daniel Yokomiso wrote:
 "Charles Hixson" <charleshixsn earthlink.net> escreveu na mensagem
 news:bm1lim$30th$1 digitaldaemon.com...
 ...
 An object Stack isn't type safe, and you need to cast the objects to their
 correct types. Also, since everything isn't an object, it can't be used with
 primitives other than object references. If you have a template Stack you
 can have both ways:...
 module stack;
 
 template TStack(T) {
     class Stack {
         ...
     }
 }
 
 alias instance TStack(Object).Stack Stack;
 
 
 The users can use an unsafe stack.Stack or instantiate their own
 stack.TStack(T).Stack
 I don't recommend creating such alias, since it endorses an unsafe style of
 programming
 
 Best regards,
 Daniel Yokomiso.
 ...

Is there a classId accessible at run time? If so one could construct a dual stack, which would know the type of the item being stored. Then the basic implementation could be of the form: class ostack { void push (Object item, int type).... void pop (Object item, int type).... int toptype () .... } and the user interface classes could be safely implemented? For that matter, what *ARE* the introspection capabilities of D?
Oct 09 2003