digitalmars.D.learn - Array of templated classes
- tsalm (11/11) Aug 18 2008 Hello,
- JAnderson (29/45) Aug 18 2008 You can't do this directly because when you try to pull the data out you...
- tsalm (4/40) Aug 18 2008 Thanks for those clear explanations.
- Wyverex (5/21) Aug 18 2008 Not sure what version of D your using but D2.0 has a variant type..
- tsalm (3/16) Aug 19 2008 I'm actually using D1.0, but the tango's support is all right. Thanks.
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
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. TSalmYou 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
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. -JoelThanks 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
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. TSalmNot 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
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.htmlI'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