Pointer to the function

To add comments, please log in or register
Petr Nosek
3024
Petr Nosek  


Is there a possibility to set a pointer to the function as a class method (not a static method)?

This example is without problems but only with a static method:

typedef void(*TAction)();

class Foo
  {
private:
   TAction  m_Action;
public:
   Foo(void)   {m_Action=Bar;}
   static void Bar()  {printf("Function %s",__FUNCTION__);}
  };

The problem is if I want to get to a member of the class thru the method. In this case, the method can't be a static method but the code doesn't work (nor it can be compiled) without static type (Error description: 'Bar' - pointer to this function type is not supported yet):

typedef void(*TAction)();

class Foo
  {
private:
   TAction  m_Action;
   bool     m_State;
public:
   Foo(void)   {m_Action=Bar;m_State=true;}
   void Bar()  {printf("State %s at %s",string(m_State),__FUNCTION__);}
  };

Is there any workaround?

lippmaje
1041
lippmaje  
class Foo
  {
typedef void(*TAction)(Foo &foo);
private:
   TAction  m_Action;
   bool     m_State;
public:
   Foo(void)   {m_Action=Bar;}
   static void Bar(Foo &foo)  {printf("State %s at %s",string(foo.m_State),__FUNCTION__);}
  };
just an idea
Petr Nosek
3024
Petr Nosek  
lippmaje:
just an idea

I can see we have a similar idea ;-) Thank you @lippmaje for your efort.

This example code (script) works well:

#property strict

class Foo
  {
private:
   typedef void(*TAction)(Foo &);
   TAction  m_Action;
   bool     m_State;
public:
               Foo(void)                     {m_Action=Bar1;m_State=true;  }
   void        Action(const TAction action)  {m_Action=action;             }
   bool        State()                       {return(m_State);             }
   void        State(const bool value)       {m_State=value;               }
   void        PrintState()                  {m_Action(this);              }
   static void Bar1(Foo &foo)
     {
      printf("State %s at %s",string(foo.State()),__FUNCTION__);
      foo.State(!foo.State());
      foo.Action(Bar2);
     }
   static void Bar2(Foo &foo)
     {
      printf("State %s at %s",string(foo.State()),__FUNCTION__);
      foo.State(!foo.State());
      foo.Action(Bar1);
     }
  };

void OnStart()
  {
   Foo foo;
   for(int i=0;i<4;i++) foo.PrintState();
  }
lippmaje
1041
lippmaje  

Ok.👍 I wonder what you guys are doing with this stuff. :)

Note that although the typedef occurs within the class, it is not bound to its scope. Using TAction outside Foo would be valid. So you should keep it in front of the private modifier to make that clear.

Petr Nosek
3024
Petr Nosek  
lippmaje:

Ok.👍 I wonder what you guys are doing with this stuff. :)

I need it for dynamically change behaviour with a possibility to change a class member.

lippmaje:

Note that although the typedef occurs within the class, it is not bound to its scope. Using TAction outside Foo would be valid. So you should keep it in front of the private modifier to make that clear.

I know about it. It was just an example. We could declare typedef outside the class (or anywhere in the class).

class Foo;

typedef void(*TAction)(Foo &);

class Foo
  {
   ...
  };
lippmaje
1041
lippmaje  
Petr Nosek:

I need it for dynamically change behaviour with a possibility to change a class member.

I know about it. It was just an example. We could declare typedef outside the class (or anywhere in the class).

Cool. The last example looks best.
To add comments, please log in or register