struct vs. class

Derek Parnell derek at psych.ward
Mon May 28 15:07:07 PDT 2007


On Mon, 28 May 2007 17:36:51 -0400, Martin wrote:

> Anyways, my point is that really the only *necessary* difference
> between structs and classes is that the latter can have run-time
> type information and virtual functions. Everything else is just
> convenient syntactical sugar for which there is no technical
> reason to not add it to structs as well.
> And if that's the case I start to wonder if it's not possible
> to unify the two concepts somehow.

They cannot and should not be 'unified' because their purpose is different.
They serve different roles for the programmer. To quote the D documentation
"Structs are meant as simple aggregations of data, or as a way to paint a
data structure over hardware or an external type." Class objects contain at
least two hidden data members which mean that they cannot be used to 'paint
over RAM', to give structure to some data. See the ABI section of the docs
...

 An object consists of:
  --------*------------------
  offset  | contents
  --------+------------------
    0     | pointer to vtable
  ptrsize | monitor

In other words an 'empty' object takes up 8 bytes (on a 32-bit machine) and
an empty struct takes up zero bytes.

-- 
Derek Parnell
Melbourne, Australia
"Justice for David Hicks!"
skype: derek.j.parnell



More information about the Digitalmars-d mailing list