struct vs. class
janderson
askme at me.com
Mon May 28 17:41:57 PDT 2007
Mike Parker wrote:
> 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.
In c++ if you define a class without any virtual it effectively becomes
a D struct, without the overhead. D takes this one step further I guess
and allows the user enforce no virtuals. (Of course the other difference
is that classes are passed by reference in D).
More information about the Digitalmars-d
mailing list