Allocating a class within another class during object init w/o passing in an allocator

David Zhang via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 15 09:44:23 PST 2016


Hello,

It is my understanding that a class can have a struct as one of 
its members, and it will be allocated in-line with the rest of 
the class' members. My question is this; how might I be able to 
do this with another class? I want to be able to allocate Foo 
using std.experimental.allocator without having to pass in a 
reference to the actual allocator. Thanks.

eg:
class SomeClass {}

class Foo {
     this(IAllocator alloc) {
         sc = alloc.make!SomeClass;
         this.alloc = alloc
     }

     ~this() {
         alloc.dispose(sc);
     }

     size_t something;
     SomeClass sc;
     IAllocator alloc
}

auto foo = alloc.make!Foo(alloc);

vs.

struct SomeStruct {}

class Foo {
     this() {
         ss = SomeStruct (...);
     }

     size_t something;
     SomeStruct ss;
}

auto foo = alloc.make!Foo;



More information about the Digitalmars-d-learn mailing list