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
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.
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.
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 { ... };
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).
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
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
- www.mql5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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:
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):
Is there any workaround?