Virtual methods on stack objects

Ali Çehreli acehreli at yahoo.com
Wed May 11 14:44:59 UTC 2022


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



More information about the Digitalmars-d-learn mailing list