www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Array of templated classes

reply tsalm <tsalm free.fr> writes:
Hello,

Does somebody know or have a way to do this :

/* --- BEGIN ---*/
class MyClass(T) {T myData;}

void main()
{
   MyClass[] myArray;
}

/* ---  END  --- */

Thanks in advance for your help.
TSalm
Aug 18 2008
next sibling parent reply JAnderson <ask me.com> writes:
tsalm wrote:
 Hello,
 
 Does somebody know or have a way to do this :
 
 /* --- BEGIN ---*/
 class MyClass(T) {T myData;}
 
 void main()
 {
   MyClass[] myArray;
 }
 
 /* ---  END  --- */
 
 Thanks in advance for your help.
 TSalm
You can't do this directly because when you try to pull the data out you have no idea what template type it is. There are several ways which might get you what you want. You could use an interface... interface MyClassI { void ProccessData(); } class MyClass(T) : MyClassI {T myData; ProccessData() {...} } void main() { MyClassI[] myArray; } You could also get at the data using a dynamic cast and I wouldn't recommend breaking polymorphism like that. Another way would be to use a void* however that means you will have to dynamic cast. A third way is to have another template manage the data: class MyTemplateArray { MyClass(int)[] myArrayInt; MyClass(float)[] myArrayFloat; ... T Get(T : int)(int index) { return myArrayInt(index); } T Get(T : float)(int index) { return myArrayFloat(index); } ... } Although I'd reconsider the design if it comes down to that. -Joel
Aug 18 2008
parent tsalm <tsalm free.fr> writes:
Le Mon, 18 Aug 2008 17:21:03 +0200, JAnderson <ask me.com> a écrit:

  Does somebody know or have a way to do this :
  /* --- BEGIN ---*/
 class MyClass(T) {T myData;}
  void main()
 {
   MyClass[] myArray;
 }
  /* ---  END  --- */
 You can't do this directly because when you try to pull the data out you  
 have no idea what template type it is.  There are several ways which  
 might get you what you want.  You could use an interface...

 interface MyClassI
 {
 	void ProccessData();
 }

 class MyClass(T) : MyClassI {T myData;  ProccessData() {...} }

 void main()
 {
 	MyClassI[] myArray;
 }

 You could also get at the data using a dynamic cast and I wouldn't  
 recommend breaking polymorphism like that.

 Another way would be to use a void* however that means you will have to  
 dynamic cast.

 A third way is to have another template manage the data:

 class MyTemplateArray
 {
 	MyClass(int)[] myArrayInt;	
 	MyClass(float)[] myArrayFloat;	
 ...
 	T Get(T : int)(int index) { return myArrayInt(index); }
 	T Get(T : float)(int index) { return myArrayFloat(index); }
 ...
 }

 Although I'd reconsider the design if it comes down to that.

 -Joel
Thanks for those clear explanations. I think using an interface could be the better way for me because I don't know the exact type of T when I get it on the array.
Aug 18 2008
prev sibling parent reply Wyverex <wyverex.cypher gmail.com> writes:
tsalm wrote:
 Hello,
 
 Does somebody know or have a way to do this :
 
 /* --- BEGIN ---*/
 class MyClass(T) {T myData;}
 
 void main()
 {
   MyClass[] myArray;
 }
 
 /* ---  END  --- */
 
 Thanks in advance for your help.
 TSalm
Not sure what version of D your using but D2.0 has a variant type.. http://www.digitalmars.com/d/2.0/phobos/std_variant.html and so does tango http://dsource.org/projects/tango/docs/current/tango.core.Variant.html
Aug 18 2008
parent tsalm <tsalm free.fr> writes:
  Does somebody know or have a way to do this :
  /* --- BEGIN ---*/
 class MyClass(T) {T myData;}
  void main()
 {
   MyClass[] myArray;
 }
  /* ---  END  --- */
  Thanks in advance for your help.
 Not sure what version of D your using but D2.0 has a variant type..
 http://www.digitalmars.com/d/2.0/phobos/std_variant.html

 and so does tango
 http://dsource.org/projects/tango/docs/current/tango.core.Variant.html
I'm actually using D1.0, but the tango's support is all right. Thanks. Forgive me for this question, but what is the difference with using "void*" ?
Aug 19 2008