   pointers to member functions within a class
  I know the whole getting-a-function-pointer-to-a-member-function thing has been beaten to death, but I think I have a new twist.  Suppose I have a class like this:class A{public:void Func1();void Func2();};  and I want to have a member variable that points to either Func1() or Func2().  I''m trying to make a finite state machine that uses pointers instead of switch/case.  Since I''m inside an instance of the class, wouldn''t it make sense if I were able to create a pointer to those functions?  I have no clue how to go about doing this, but it seems like it should be possible, because it doesn''t have the problems that getting a pointer to a member function outside of the class has.   --------------------You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming You are unique.  Just like everybody else."Mechanical engineers design weapons; civil engineers design targets.""Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensorYanroy@usa.com 
  Well you could try this: typedef void (__closure *METHOD_POINTER)(void);class myClass{  protected:    METHOD_POINTER myMethodPointer;  public:    myClass()    { myMethodPointer = myFunction    } void myMethod(){}};Using the __closure keyword, you can even make your method pointer point to NULL, like this:    typedef void (__closure *METHOD_POINTER)(void);class myClass{  protected:    METHOD_POINTER myMethodPointer;  public:    myClass()    { myMethodPointer = NULL;    }void setPointer(METHOD_POINTER ptr){  if(myMethodPointer == NULL) myMethodPointer = ptr;    } void myMethod(){}};myClass cls;cls.setPointer(cls.myMethod);However I don't know if the __closure keyword is compiler specific...    [edited by - angry on July 31, 2003 12:36:46 PM];  Maybe I'm missing something, but if "the whole getting-a-function-pointer-to-a-member-function thing has been beaten to death" then how can you possibly not understand how to make that pointer a member variable? class A{public:  void (A::*m_pt2Member)();  A() : m_pt2Member(this->func1) {}  void func1();  void func2();  void swapFuncs() {    m_pt2Member = (m_pt2Member == func1 ? func2 : func1);  }};Regards,JeffEdited: As per bslayerw's comment below.  cgoat also caught one...   [edited by - rypyr on July 31, 2003 6:33:26 PM];  __closure is compiler specific (the __ gives it away) to Borland compilers.What the original poster is asking for is covered by standard C++ and thus there is no reason to use nonstandard extensions. ;  rypyr, I think you have a syntax error in your code. Shouldn''t it be:  m_pt2Member = (m_pt2Member == func1() ? func2() : func1());and not  m_pt2Member = (m_pt2Member == func1() : func2() : func1());;  agh...yep...thx ;  Btw, I've thought myself about using function pointers to do a state machine in hopes of saving cycles per frame by avoiding if/switches.  However, I haven't convinced myself that it's worthwhile: void render() {  if(m_bLightingEnabled) {    prepareLights();  }  if(m_bHardware) {    prepareHardware();    renderHardware();  }  else {    renderSoftware();  }  // etc...}Now if you want to do this using function pointers you might decide to use a vector of function pointers and set them up before hand (i.e. called once): void setupStateMachine() {  m_vFnPtrs.clear();  if(m_bLightingEnabled) {    m_vFnPtrs.push_back(prepareLights);  }  if(m_bHardware) {    m_vFnPtrs.push_back(prepareHardware);    m_vFnPtrs.push_back(renderHardware);  }  else {    m_vFnPtrs.push_back(renderSoftware);  }  // etc..}and make your render() function this simple and hopefully "faster":   void render() {  std::vector<FNPTR>::iterator itFn = m_vFnPtrs.begin();  std::vector<FNPTR>::const_iterator itEnd = m_vFnPtrs.end();  while(itFn != itEnd) {    (*itFn++)();  }}However, this means that you still have to do an "if" for every function pointer executed (i.e. the while end condition), a dereference for every iterator, a dereference for every pointer and an increment for every iterator.  Does it really save me anything more than the ifs above in the original implementation?  In this case, if bHardware is true, you do three ifs, six dereferences, three function calls and three increments.  In the first approach, you only did two ifs and three function calls.Of course it depends on how complicated your state machine is.  I'll agree that your render() function becomes very clean-looking, but is it better?Anybody else have any ideas along this line?Regards,JeffEdit:  Changed code tags to source tags...  [edited by - rypyr on July 31, 2003 1:38:51 PM];quote:Original post by rypyr  Btw, I''ve thought myself about using function pointers to do a state machine in hopes of saving cycles per frame by avoiding if/switches.  However, I haven''t convinced myself that it''s worthwhile: void render() {  if(m_bLightingEnabled) {    prepareLights();  }  if(m_bHardware) {    prepareHardware();    renderHardware();  }  else {    renderSoftware();  }  // etc...}Now if you want to do this using function pointers you might decide to use a vector of function pointers and set them up before hand (i.e. called once):   void setupStateMachine() {  m_vFnPtrs.clear();  if(m_bLightingEnabled) {    m_vFnPtrs.push_back(prepareLights);  }  if(m_bHardware) {    m_vFnPtrs.push_back(prepareHardware);    m_vFnPtrs.push_back(renderHardware);  }  else {    m_vFnPtrs.push_back(renderSoftware);  }  // etc..}and make your render() function this simple and hopefully "faster":    void render() {  std::vector<FNPTR>::iterator itFn = m_vFnPtrs.begin();  std::vector<FNPTR>::const_iterator itEnd = m_vFnPtrs.end();  while(itFn != itEnd) {    (*itFn++)();  }}However, this means that you still have to do an "if" for every function pointer executed (i.e. the while end condition), a dereference for every iterator, a dereference for every pointer and an increment for every pointer.  Does it really save me anything more than the ifs above in the original implementation?  In this case, if bHardware is true, you do three ifs, three dereferences and three increments whereas you only did two ifs in the first approach.  Of course it depends on how complicated your state machine is.  I''ll agree that your render() function becomes very clean-looking, but is it better?Anybody else have any ideas along this line?Regards,JeffEdit:  Changed code tags to source tags...  [edited by - rypyr on July 31, 2003 1:32:47 PM]I wrote a byte-code interpreter once... I though the same thing as you... hey, I''ll use an array of pointers to functions instead of a case statement... but, I found out that a case statement (when put in the proper order and I had all functions inlined) gets converted to a jump table, which ended up being a good amount faster than my function pointer table, and faster than the switch statement without inlining (and I even wrote a custom jump table with a few small hacks in MSVC and it was faster than that too!). ;quote:Original post by Ready4Dis  I wrote a byte-code interpreter once... I though the same thing as you... hey, I''ll use an array of pointers to functions instead of a case statement... but, I found out that a case statement (when put in the proper order and I had all functions inlined) gets converted to a jump table, which ended up being a good amount faster than my function pointer table, and faster than the switch statement without inlining (and I even wrote a custom jump table with a few small hacks in MSVC and it was faster than that too!).   Yeah.  Interesting idea about a custom jump table... ;  Actually, this:m_pt2Member = (m_pt2Member == func1() ? func2() : func1());  Should be:m_pt2Member = (m_pt2Member == func1 ? func2 : func1);  No parentheses, otherwise it''s calling the function, not assigning the function pointer. 