www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Class Tempates

reply Ark <Ark_member pathlink.com> writes:
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
parent reply Frank Benoit <benoit__ __tionex.de> writes:
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
parent reply Ark <Ark_member pathlink.com> writes:
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
parent reply Frank Benoit <benoit__ __tionex.de> writes:
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
parent reply Kyle Furlong <kylefurlong gmail.com> writes:
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
Apr 02 2006
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
prev sibling parent Frank Benoit <benoit__ __tionex.de> writes:
     StackS ss = StackS;
Wait, does this work now? I though we didn't have stack based classes yet.
Oops, type. new StackS
Apr 03 2006