[Understanding] Classes and delegate inheritance vs function pointers

Jacob Carlborg doob at me.com
Sat Jan 9 20:00:35 UTC 2021


On 2021-01-09 19:16, Q. Schroll wrote:
> Say I have a class hierarchy like this:
>    class Base { }
>    class Derived : Base { }
> A Derived object cannot be referenced as a Base object, but as a 
> const(Base) object. That makes sense to me.

It can:

Base b = new Derived();

> One can replace Base by a @system delegate type (SysDG) and Derived by a 
> @safe delegate type (SafeDG) and it works the same way: a SafeDG object 
> cannot be referenced as a SysDG object, but as a const(SysDG) object.
> 
> However, if I try that with function pointers instead of delegates 
> (SysFP, SafeFP), for some reason, a SafeFP cannot be referenced as a 
> const(SysFP).
> This makes no sense in my head. Is there some reason I'm unable to see?
> 
> Example code is here: https://run.dlang.io/is/zSNArx

Is there a reason all you're examples are using pointers? Classes are 
already reference types, delegates are kind of like reference types. 
They consist of a context pointer and a function pointer. Function 
pointer are, as the name suggest, already pointers.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list