Get class size of object

JS js.mdnq at gmail.com
Mon Aug 12 08:49:54 PDT 2013


On Monday, 12 August 2013 at 11:34:25 UTC, Jacob Carlborg wrote:
> 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);
> }

Sorry but this doesn't work. b is a B, not an A.

try

I a = new A;
I b = new B;







More information about the Digitalmars-d-learn mailing list