views:

85

answers:

2

I want the following struct as a class member, but I don't know the type of T, so I need "declare" the struct at runtime.


struct Chunk (T) {
    string id;
    T[][] data;
}

class FileBla {
    this() {
        Chunk !int ck; // need to be turned in a class member
    }
}

Should be missing something easy.

+8  A: 

You can template the class as well:

import std.stdio;

struct Chunk (T) {
    string id;
    T[][] data;
}

class FileBla(T) {
private:
    Chunk!T ck;
}

void main() {
    auto f = new FileBla!int;
    writeln(typeid(f.ck));
}
van
+2  A: 

I'll assume you're used to programming in dynamic languages and are now trying to learn a static language.

There are at least three reasonable ways to do this:

Template FileBla, too:

class FileBla(T) {
    Chunk!T ck;

    // Other stuff.
}

Wrap Chunk in a polymorphic class.

Allocate Chunk on the heap and store a void* pointer to it. This is the old C-style way to do things, will require manually casting the pointer to the correct type, and is not memory safe. Nonetheless it is occasionally useful.

dsimcha