How to overload member function pointer and a regualr member function

ParticlePeter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 24 09:46:21 PDT 2017


I would like to have this kind of struct:

struct Foo {
   private int i;
   void function( int i, float f ) bar;  // will be defined at 
runtime
   void bar( float f ) {
     bar( i, f );
   }
}

But apparently the function pointer and the member function 
cannot have the same name: Error: function main.Foo.bar conflicts 
with variable main.Foo.bar ...

I tried with an inner struct:
struct Foo {
   private int i;
   void function( int i, float f ) bar;  // will be defined at 
runtime
   private struct Inner {
     void bar( float f ) {
       bar( i, f );
     }
   }
   Inner inner;
}

But this time I get following error:
Error: need 'this' for 'i' of type 'int'

What does this message tell me? Should the inner struct not be 
able to access Foo.i?

How else can I get the required behavior?

I would prefer to avoid another indirection like this:
struct Foo {
   private int i;
   void function( int i, float f ) bar;  // will be defined at 
runtime
   void baz( float f ) {
     bar( i, f );
   }
   void baz( int ii, float f ) {
     bar( ii, f );
   }
}



More information about the Digitalmars-d-learn mailing list