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

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 15 13:47:15 PST 2016


On Thursday, 15 December 2016 at 21:37:34 UTC, David  Zhang wrote:
> So the size of Foo would be the size of SomeClass plus members? 
> ie. Is the size of the array stored too?

With these definitions:

----
class SomeClass {}

class Foo
{
     this()
     {
         import std.conv: emplace;
         emplace!SomeClass(scStorage);
     }

     @property SomeClass sc() { return cast(SomeClass) 
scStorage.ptr; }
     void[__traits(classInstanceSize, SomeClass)] scStorage;
}
----

the "instance size" of Foo is the size of

a) Foo's hidden/implicit fields which all classes have, plus
b) the size of scStorage which is Foo's only explicit field.

The size of scStorage is the instance size of SomeClass (just the 
implicit fields here). The size/length of scStorage is known at 
compile-time. It's not stored in Foo.


More information about the Digitalmars-d-learn mailing list