On Sunday, 15 May 2022 at 15:26:40 UTC, Kevin Bailey wrote:
I've done some scripting in D over the years but I never dug into D until recently. I'm going through Learning D and I was reminded that structs and classes are so different.

- struct methods are non-virtual while class methods are virtual
- Thus, structs can't inherit, because how would you find the child's destructor given a parent pointer?
- On the stack, structs by-value but classes are by-reference

I'm trying to understand why it is this way. I assume that there's some benefit for designing it this way. I'm hoping that it's not simply accidental, historical or easier for the compiler writer.

A virtual function call has to pass through a virtual function look-up, and thus there is (some) overhead involved.

Thus, by design, structs avoid this overhead (completely).

It's also (I think) another reason why D does not support multiple inheritance. Since you would need multiple virtual function tables.

Reply via email to