structs vs classes

Jim bitcirkel at yahoo.com
Sat Jan 29 07:12:13 PST 2011


Simen kjaeraas Wrote:

> Jim <bitcirkel at yahoo.com> wrote:
> 
> > I'm only discussing the heap/stack difference.
> 
> In D you are allowed to safely put your structs on the heap, and
> unsafely put your classes on the stack. What more do you want?
> 
> Also, a D struct is POD. It has no vtable, it does not support
> subtyping except via alias this, and it is simply a different
> beast from classes. This is a good thing, as you often want such
> a light-weight abstraction. How would you suppose we retain this
> if we were to abolish this dichotomy?
> 
> -- 
> Simen

Oh?

"All class-based objects are dynamically allocated—unlike in C++, there is no way to allocate a class object on the stack."
- The D Programming Language, chapter 6.

The lightweight nature of structs is very appealing though. I like that very much of course. Couldn't that be optimised by the compiler alone knowing that a class wasn't derived?


More information about the Digitalmars-d mailing list