struct vs. class

Mike Parker aldacron71 at yahoo.com
Mon May 28 17:38:38 PDT 2007


Martin wrote:

> 
> There is no reason why structs couldn't have hierarchies as well.
> 
>> Your 'b' statement is incorrect. In both cases, the data is allocated 
>> where the struct/class is allocated. Just classes are usually on the 
>> heap, structs are generally on the stack.
>>
> Oh, then I got that wrong. I thought there was no way in D to create a class variable on the stack.
> 
> 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.

When you want POD (plain old data) objects, with or without methods to 
manipulate them, it is nice to not have the overhead associated with 
classes. It also would be a bit cumbersome to interface with C code if 
structs had all of the class overhead.



More information about the Digitalmars-d mailing list