Why are structs and classes so different?

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sun May 15 20:35:48 UTC 2022


On Sunday, 15 May 2022 at 20:05:05 UTC, Kevin Bailey wrote:
> I've been programming in C++ full time for 32 years, so I'm 
> familiar with slicing. It doesn't look to me like there's a 
> concern here.

Yes, slicing is not the issue. Slicing is a problem if you do 
"assignments" through a reference that is typed as the 
superclass… so references won't help.

The original idea might have been that structs were value-types, 
but that is not the case in D. They are not, you can take the 
address…

So what you effectively have is that structs follow C layout 
rules and D classes are not required to (AFAIK), but there is an 
ABI and C++ layout classes, so that freedom is somewhat limited… 
D classes also have a mutex for monitors.

In an ideal world one might be tempted to think that classes were 
ideal candidates for alternative memory management mechanisms 
since they are allocated on the heap. Sadly this is also not true 
since D is a system level programming language and you get to 
bypass that "type characteristic" and can force them onto the 
stack if you desire to do so…







More information about the Digitalmars-d-learn mailing list