Pointer to the function

 


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?

 
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
 
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();
  }
 

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.

 
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
  {
   ...
  };
 
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.
 

why not do simple

class Foo
{
private:
   
public:
   bool     m_State;
   Foo()   {m_State=false;}
};

void OnStart() {
   Foo foo;
   //FchgState(foo);
   FchgState(foo);
   Print((string)foo.m_State );
   FchgState(foo);
   Print((string)foo.m_State );
   FchgState(foo);
   Print((string)foo.m_State );
   FchgState(foo);
   Print((string)foo.m_State );
}

void FchgState(Foo &foo){
  foo.m_State=!foo.m_State;
}

at least you will work with one instance of your class...

and your functor accepting foo & being used from class Foo - is accepting anybody (any foo) & even itself as a parameter -- this violates encapsulation anyway... imho

 
JeeyCi #:

why not do simple..... -- this violates encapsulation anyway...

aaa,.. my "simple" also violates OOP nature... m_state=true & m_state=false are being different objects (& if being the same with dual-face possible & besides left reversible-back [if complex object] - it would not be thread-safe I think)... understood... therefore pattern State exists

state - behavioral design pattern
state - behavioral design pattern
  • www.mql5.com
allow an object to alter its behavior when its internal state changes. the object will appear to change its class