struct vs. class

Martin m_dot_hinsch at rug.nl
Tue May 29 02:15:05 PDT 2007


Derek Parnell Wrote:

> 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

I know, but the differences between structs and classes go far beyond that. In my opinion that wouldn't be necessary. Or put the other way around I think it would be much more elegant (and make D easier to learn) to have aggregate types, RTTI and value/reference semantics as three orthogonal concepts. C++ does that by using virtual vs. the default and pointers/references vs. values with all the associated problems but there might be a cleaner and nicer way without having to give up the flexibility.



More information about the Digitalmars-d mailing list