delegates - C++/CLI managed class member callback by managed code -


i designing class in c++/clr uses callback function provided user code.

this works nicely if callback function free ( i.e. not member of class ). same in pure c++.

here sample code works well:

ref class classthatusescallback { public:     typedef void (*callback_t)( string^ );     void setcallback( callback_t pfun )     {         mycallback = pfun;     }     void run()     {         if( mycallback != nullptr ) {             mycallback("this test");         }     } private:     callback_t mycallback; };  void freefunction( string^ s ) {     console::writeline( "free function callback " + s ); }  int main(array<system::string ^> ^args) {    classthatusescallback^ theclassthatusescallback        = gcnew classthatusescallback();     theclassthatusescallback->setcallback( freefunction );     theclassthatusescallback->run(); } 

however, callbacked function member of class in user code ( can make use of , change attributes of user code class )

the following code not compile

ref class classthatprovidescallback { public:     void memberfunction( string^ s )     {         console::writeline( "member function callback " + s );     }     void run()     {         classthatusescallback^ theclassthatusescallback             = gcnew classthatusescallback();         theclassthatusescallback->setcallback(              &classthatprovidescallback::memberfunction );         theclassthatusescallback->run();     }  }; 

i error

error c3374: can't take address of 'classthatprovidescallback::memberfunction' unless creating delegate instance 

when research this, find numerous explanations of how call un-managed code managed code ( , vice-versa ) not need - code involved managed. hoping can point me simple way this.

this full solution:

ref class classthatusescallback { public:     void setcallback( action<string^>^ callback )     {         mycallback = callback;     }     void run()     {         if( mycallback != nullptr ) {             mycallback("this test");         }     } private:     action<string^>^ mycallback; };  ref class classthatprovidescallback { public:     void memberfunction( string^ s )     {         console::writeline( "member function callback " + s );     }     void run()     {         classthatusescallback^ theclassthatusescallback             = gcnew classthatusescallback();         theclassthatusescallback->setcallback(gcnew action<string^>(this,             &classthatprovidescallback::memberfunction));         theclassthatusescallback->run();     }  };  int main(array<system::string ^> ^args) {     classthatprovidescallback^ c = gcnew classthatprovidescallback();     c->run();      return 0; } 

native c++ style typedef replaced .net action delegate. additional parameter this added setcallback call, required define class instance contains callback function.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -