Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1096

 

How do I create a pointer toa class method?

class qer{
public:
int _int(int q){...}
int int_(int q){...}
};qer *_qer

You need to create a pointer to a_int andint_ function.

In mql4 something is described about pointers and classes here(https://docs.mql4.com/ru/basis/types/this,https://docs.mql4.com/ru/basis/types/object_pointers,https://docs.mql4.com/ru/common/getpointer) but there are no comments, it's not clear what it's for and what it does. Without classes it's easy to declare a pointer using typedef but I want to understand how it works in classes.

Trying to apply typedef to a class function gets an error pointer to this function type is not supported yet, tried the instructions above but since it's not shown for what and no comments, nothing works.
 
Seric29:

How do I create a pointer toa class method?

You need to create a pointer to a_int andint_ function.

In mql4 something is described about pointers and classes here(https://docs.mql4.com/ru/basis/types/this,https://docs.mql4.com/ru/basis/types/object_pointers,https://docs.mql4.com/ru/common/getpointer) but there are no comments, it's not clear what it's for and what it does. Without classes it's easy to declare a pointer using typedef, but I'd like to understand how it works in classes.

Trying to apply typedef to a class function, error pointer to this function type is not supported yet, tried the instructions above but since it's not shown what it's for and no comments, nothing works.
Can't
 
 
Wrong. A person needs a pointer to a method of a class, not a descendant conversion.
 
Vladimir Simakov:
Wrong. A person needs a pointer to a method of a class, not a cast to an inheritor.

there is no other - either procedural style without access control or OOP with access and type control

ok, never mind, the author always has highly specialised problems ;)

 
Seric29:

How do I create a pointer toa class method?

You need to create a pointer to a_int andint_ function.

In mql4 something is described about pointers and classes here(https://docs.mql4.com/ru/basis/types/this,https://docs.mql4.com/ru/basis/types/object_pointers,https://docs.mql4.com/ru/common/getpointer) but there's no comments, it's not clear what it's for and what it does. Without classes it's easy to declare a pointer using typedef, but I'd like to understand how it works in classes.

Trying to apply typedef to a class function gets an error pointer to this function type is not supported yet, tried the instructions above but since it's not shown for what and no comments, nothing works.

Just wondering: how do you plan to use it?

 

Why do you need to save a pointer to the whole class, here's an examplehttps://docs.mql4.com/ru/basis/types/this

//+------------------------------------------------------------------+
//| возвращает собственный указатель                                 |
//+------------------------------------------------------------------+
CDemoClass *CDemoClass::getDemoClass(void)
  {
   return(GetPointer(this));
  }

What to do with it afterwards?

 
Vladimir Simakov:
You can't

The question arises: if you can't do that, why do you write functions in a class that take functions? And besides, sometimes you need to implement an interface and sometimes you don't know which function to call.

 
Seric29:

The question arises: if you can't do that, why do you write functions in a class that take functions? And besides, sometimes you need to implement an interface and sometimes you don't know which function to call.

How does this work?

 
Vladimir Simakov:

How does it work?

A pointer to a function is passed as a parameter. I see no one has figured out how to do this with classes. It's also still not clear to me why a pointer to a class should be returned.

Here are interesting samples but they are not related to my question.

#property strict

class CStrategy;
typedef bool (*SignalFn)(CStrategy*);

// Global function definitions
bool IsBuySignal(CStrategy*ptr) { return ptr.IsBuySignal(); }
bool IsSellSignal(CStrategy*ptr) { return ptr.IsSellSignal(); }

// Create an abstract base class
class CStrategy
{
public:
        virtual bool IsBuySignal(void)=0;
        virtual bool IsSellSignal(void)=0;
};

// Derived class
class Strat1 : public CStrategy
{
public:
        // Implement the required functions
        bool IsBuySignal(void) { return true;}
        bool IsSellSignal(void) { return true;}
};

void OnStart()
{
        // instantiate a class
        Strat1 strat1;

        // Global functions work as expected
        SignalFn ptrSignalFn;                           // pointer to a signal function
        ptrSignalFn = IsBuySignal;
        if ( ptrSignalFn(&strat1) ) { Print("Is Buy"); }
}
I have also seen something similar in the guide, but I have already closed these sites as my hands hurt and I have work to do.
Reason: