digitalmars.D - Class Tempates
- Ark (25/25) Apr 02 2006 Hello,
- Frank Benoit (21/21) Apr 02 2006 Haven't compiled it. But should work.
- Ark (6/6) Apr 02 2006 Hello Frank,
- Frank Benoit (33/44) Apr 02 2006 You can also write this without auto and with template:
- Kyle Furlong (2/49) Apr 02 2006
- Jarrett Billingsley (5/6) Apr 02 2006 Haha :)
- Frank Benoit (1/5) Apr 03 2006 Oops, type. new StackS
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");
}
Apr 02 2006
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;
}
Apr 02 2006
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.
Apr 02 2006
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
Apr 02 2006
Frank Benoit wrote:Ark schrieb:Wait, does this work now? I though we didn't have stack based classes yet.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
Apr 02 2006
"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.
Apr 02 2006
Oops, type. new StackSStackS ss = StackS;Wait, does this work now? I though we didn't have stack based classes yet.
Apr 03 2006









"Jarrett Billingsley" <kb3ctd2 yahoo.com> 