Get class size of object

Jacob Carlborg doob at me.com
Mon Aug 12 04:34:24 PDT 2013


On 2013-08-11 21:08, Simen Kjaeraas wrote:

> If you're looking for a no-overhead solution, then this might not be
> good enough. I'm surprised that a virtual function call is fine,
> though.

How about this:

interface I
{
     size_t size (this T) ()
     {
         return __traits(classInstanceSize, T);
     }
}

class A : I
{
}

class B : I
{
     int a;
}

void main ()
{
     auto a = new A;
     auto b = new B;

     assert(a.size == 24);
     assert(b.size == 32);
}

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list