   Function Pointers & Delegates in C++...Whats wrong with this code?
  i was readig a tutorial in http://www.cuj.com  named "Inside .NET's and Events Using C++" .But when i tried to compile the code i got an error.Code is below can you understand what is the cause?#include  <iostream>#include  <stdio.h>using namespace std;////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////&#305;s function pointer safe or not//////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////**********//*****AMAÇ:  Üye fonksiyon i&#351;aretçisiyle nesneyi birle&#351;tirmek//**********/**/typedef size_t (*FUNC) (const char*); //TYPEDEF FUNCTION POINTER...void printSize(const char* str){FUNC f=strlen; //Fonksiyon i&#351;aretçisine  strlen fonksiyonunun adresini atad&#305;kprintf("%s is %ld char\n",str,f(str));  //we used the fucntion pointer} void crashAndBurn(const char* str){FUNC f=reinterpret_cast<FUNC>(strlen);   //NO PROBLEM.signature of strlen and function pointer is similiar    //we can use any type of cast including reinterpret_cast (we can make coersion)//f=reinterpret_cast<FUNC>(strcat);   //if we would do that we got an assertion ,signature of strcat and    //function pointer is not same..NEVER USE COERSION(reinterpret_cast)f(str);}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////MemberFucntion  safety   //////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////class CObject;typedef size_t (CObject::*MEMBERFUNC)(const char*) ; //we made a member function pointerclass CObject{};class Str:public CObject{public:size_t Len(const char* str){return (strlen(str));}char* Cat(char* S1,const char* S2){return (strcat(S1,S2));}};void MemberPrintSize(const char* s){Str str;MEMBERFUNC f=static_cast<MEMBERFUNC>(&Str::Len);  //we assigned function to a pointer to memberfunctionprintf("%s is % ld chars\n",s,(str.*f)(s));  //we used our pointer to member function..no problem  }/*void  MemberCrashAndBurn(const char* s){Str str;//MEMBERFUNC f=static_cast<MEMBERFUNC>(&Str::Cat); //if we try to use static_cast here ...it will warn us(str.*f)(s);  //function pointer and function's signatures are different}*///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////MAK&#304;N A DELEGATE //////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////class Strlen_Delegate{public:typedef size_t (Str::*MF_T) (const char*) ;   //lets create a function pointerMF_T m_method;  //lets define a function pointerCObject& m_pTarget;   //this will be our object  (CObject is base class of Str) //s&#305;n&#305;f&#305; sakl&#305;yal&#305;mStrlen_Delegate(CObject& AnyObject,const MF_T& mf):m_pTarget(AnyObject),m_method(mf){}MF_T Method() const   {return m_method;}CObject& Target() const   {return m_pTarget;}size_t Invoke(const char* s)//TH&#304;S &#304;S THE MOST IMPORTANT METHOD OF DELEGATE ..INVOKE METHOD{  (m_pTarget.*m_method)(s); // PROBLEEEEEMMMM   WHY???? AND WHAT'S THE SOLUTION}};void MyPrintSize2(const char* s){Str str;Strlen_Delegate d(str,&Str::Len);printf("%s is %ld chars\n",s,d.Invoke(s));}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////    ma&#305;n function   //////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////int main(int argc,char* argv[]){/*  char* Serdar="Serdar Osmanagaoglu";printSize(Serdar);crashAndBurn(Serdar);*//*  */MyPrintSize2("Serdar");//  PROGRAMDAN ÇIKI&#350;char ks='0';cout<<"\nPress AnyKey to exit";cin>>ks;return (0) ;}[Edited by - tonymontana on January 6, 2006 2:47:47 AM]
  1) Please use the [ source ] [ /source ] tags.2) Please rewrite comments in English (and use less[wink]).Regards,Pat   ;  Strlen_Delegate stores a pointer to a CObject and then tries to invoke a method of the derived class, Str, on that CObject. This results in a compiler error. You could avoid the problem by removing the CObject class, and replacing all mentions of CObject with Str. (Also, Invoke() needs a return statement.)If you need more information on function pointers, see function-pointer.org. You might also want to look into boost::function at some point.   