Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
April 02, 2006 Class Tempates | ||||
---|---|---|---|---|
| ||||
Hello, How do you use template class instance ? For instance how do you translate this generic stack template ? Thanks, Ark ----------------------- template <class T> class Stack { public: Stack() { top = -1; } void push(T i) { st[++top] = i; } T pop() { return st[top--]; } private: int top; T st[100]; }; int main () { Stack<int> ii; Stack<string> ss; ii.push(25); ss.push("Hello"); } |
April 02, 2006 Re: Class Tempates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ark | Haven't compiled it. But should work. class Stack (T) { public: this() { top = -1; } void push(T i) { st[++top] = i; } T pop() { return st[top--]; } private: int top; T[100] st; }; int main ( char[][] args) { auto ii = new Stack!(int); auto ss = Stack!(char[]); ii.push(25); ss.push("Hello"); return 0; } |
April 02, 2006 Re: Class Tempates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank Benoit | Hello Frank, It works ! I realize now that i missed two points : First i used template Stack (T) instead of class Stack(T) Second i missed the auto keyword... Thanks. |
April 02, 2006 Re: Class Tempates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ark | Ark schrieb: > Hello Frank, > > It works ! > > I realize now that i missed two points : > First i used template Stack (T) instead of class Stack(T) > Second i missed the auto keyword... > > Thanks. > > You can also write this without auto and with template: template(T) { class Stack { public: this() { top = -1; } void push(T i){ st[++top] = i; } T pop(){ return st[top--]; } private: int top; T[100] st; } } int main ( char[][] args) { alias Stack!(int) StackI; alias Stack!(char[]) StackS; StackI ii = new StackI; StackS ss = StackS; ii.push(25); ss.push("Hello"); return 0; } For more information see: http://www.digitalmars.com/d/template.html |
April 02, 2006 Re: Class Tempates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Frank Benoit | Frank Benoit wrote: > Ark schrieb: >> Hello Frank, >> >> It works ! >> >> I realize now that i missed two points : >> First i used template Stack (T) instead of class Stack(T) >> Second i missed the auto keyword... >> >> Thanks. >> >> > > You can also write this without auto and with template: > template(T) > { > class Stack > { > public: > this() { > top = -1; > } > void push(T i){ > st[++top] = i; > } > T pop(){ > return st[top--]; > } > private: > int top; > T[100] st; > } > } > > int main ( char[][] args) > { > alias Stack!(int) StackI; > alias Stack!(char[]) StackS; > StackI ii = new StackI; > StackS ss = StackS; Wait, does this work now? I though we didn't have stack based classes yet. > ii.push(25); > ss.push("Hello"); > return 0; > } > > For more information see: > http://www.digitalmars.com/d/template.html |
April 03, 2006 Re: Class Tempates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kyle Furlong | "Kyle Furlong" <kylefurlong@gmail.com> wrote in message news:e0pj2h$1c8r$1@digitaldaemon.com... > Wait, does this work now? I though we didn't have stack based classes yet. Haha :) Wrong kind of stack. This is just a stack data structure. We don't have classes which can be allocated on the program stack. |
April 03, 2006 Re: Class Tempates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kyle Furlong |
>> StackS ss = StackS;
>
> Wait, does this work now? I though we didn't have stack based classes yet.
>
Oops, type. new StackS
|
Copyright © 1999-2021 by the D Language Foundation