Covariant callback functions, or assigning base class members through a subclass reference
    Jacob Carlborg via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Tue Jul 14 23:22:04 PDT 2015
    
    
  
On 2015-07-14 17:28, Rene Zwanenburg wrote:
> Given the following code:
>
> class Base
> {
>      alias CallbackType = void delegate(Base);
>
>      CallbackType callback;
>
>      void foo()
>      {
>          callback(this);
>      }
> }
>
> class Derived : Base
> {
>
> }
>
> void main()
> {
>      auto d = new Derived();
>      d.callback = (Derived d) { /* Do something */ }
> }
>
> Obviously this won't compile, since the callback function needs to have
> Base as parameter, not Derived.
You can cast the delegate. It's probably unsafe but a simple example works.
-- 
/Jacob Carlborg
    
    
More information about the Digitalmars-d-learn
mailing list