Virtual methods on stack objects

Marvin Hannott i28muzw0n at relay.firefox.com
Wed May 11 20:06:44 UTC 2022


On Wednesday, 11 May 2022 at 14:44:59 UTC, Ali Çehreli wrote:
> On 5/11/22 06:57, Marvin Hannott wrote:
>
> > I understand that D's classes solve some particular problems.
> However,
> > they also cause problems because they cannot be copied when
> scoped. So
> > how do I get virtual methods on a copyable stack object?
>
> Problems classes solve don't usually need copying nor need to 
> be on the stack. Is that really necessary?
>
> If so, I can think of two solutions:
>
> a) Use a struct but provide "virtual" functions manually (with 
> function pointers like one would do in C, all of which 
> potentially collected in a per-type function table (a.k.a. vtbl 
> :) )).
>
> b) Use classes but provide a virtual function that you call to 
> copy.
>
> Ali

I appreciate the answer, don't much like the "solutions".

It's not so much about copying, but about heap allocations. Tying 
virtual methods to heap allocation is kind of unfortunate. And I 
am not really keen on building a poor man's vtbl.
And for my taste, scoped class objects should have value 
semantics.


More information about the Digitalmars-d-learn mailing list