[Understanding] Classes and delegate inheritance vs function pointers

Q. Schroll qs.il.paperinik at gmail.com
Sat Jan 9 20:20:38 UTC 2021


On Saturday, 9 January 2021 at 20:00:35 UTC, Jacob Carlborg wrote:
> 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();

That's not what I mean. You copy the reference. That's not what 
referencing meant.

   Derived d = new Derived();
   Base* bp = &d; // fails
   const(Base) cbp = &d; // compiles.

> Is there a reason all you're examples are using pointers?

Yes. Actually, I need it for slices, but only pointer part of it 
really mattered.

A Derived[] is implicitly a const(Base)[], not a Base[].
A void delegate() @safe[] is implicitly a const(void 
delegate())[].
But it seems a void function() @safe[] **isn't** implicitly a 
const(void function())[].

Functions taking those are kind of useless like that.


More information about the Digitalmars-d-learn mailing list