Home > Enterprise >  how to deal with a function pointer problem?
how to deal with a function pointer problem?

Time:05-20

i'm implementing a normal function pointer.

so this is the function that i want to call:

  WndDyn* Punkt2d::pEditPunkt(WndInfo& wi, Int32 AnzSichtChar, Bool WithUnit, 
  const DecimalsConf& DecConf)
  {
     WynDyn_callback Dyncallback;
     Dyncallback.AnzSichtChar = AnzSichtChar;
     Dyncallback.WithUnit = WithUnit;
     Dyncallback.DecConf = DecConf;

     return &(DlgZeile(wi)
              pEditAll(Dyncallback, &pEditFeldX)//pEditFeldX(AnzSichtChar, WithUnit, 
              DecConf)
              FntXUnit(2)
              pEditFeldY(AnzSichtChar, WithUnit, DecConf)
             );
   }

After defining the function that needs to be called i defined my callee as follow:

WndDyn*   pEditAll(WynDyn_callback& Dyncallback, WndDyn* (func_Call) 
      (WynDyn_callback)) {
        return  func_Call(Dyncallback);
    }

And last of all this is the function that needs to be called using the callee function:

WndDyn* Punkt2d::pEditFeldX(WynDyn_callback Dyncallback) {

    return &Edit(pNewStrDataLink(m_x, DLUC_Length, Dyncallback.DecConf), 
                 Dyncallback.AnzSichtChar)
                 .WithSelAllOnFocus(True);
    }

My actuall problem is that my compiler is underlining the function pEditFeldX in this line pEditAll(Dyncallback, pEditFeldX) in the function pEditpunkt and showing me this Error:

Severity Code Description Project File Line Suppression State Error C3867 'Punkt2d::pEditFeldX': non-standard syntax; use '&' to create a pointer to member

Severity Code Description Project File Line Suppression State Error (active) E0167 argument of type "WndDyn (Punkt2d::)(WynDyn_callback Dyncallback)" is incompatible with parameter of type "WndDyn ()(WynDyn_callback)"

CodePudding user response:

Because pEditFeldX is a member function you can't just call pEditFeldX(Dyncallback). You must call the function on some Punkt2d object, using e.g. meinPunkt2d.pEditFeldX(Dyncallback).

If you write pEditFeldX(Dyncallback) inside the Punkt2d class then it means (*this).pEditFeldX(Dyncallback). The compiler adds (*this). to save some typing.

A function pointer only points to a function. It doesn't point to a function and an object. It points to pEditFeldX, not meinPunkt2d.pEditFeldX. You must specify the Punkt2d object when you call it.

To remember that a Punkt2d must be specified, a function pointer which points to a member function is declared as this: WndDyn* (Punkt2d::*func_Call)(WynDyn_callback) and called as this: meinPunkt2d.*func_Call(Dyncallback);

If the function pointer is &pEditFeldX then meinPunkt2d.*func_Call(Dyncallback) is the same as meinPunkt2d.pEditFeldX(Dyncallback)

This doesn't apply to static member functions. Static member functions can be used with normal function pointers since no object is required.


It is not quite clear what you are trying to do, but if I understand it right, I think that std::function would be able to solve your problem std::function is able to store anything which can be called, including "half of a function call" like you seem to want. std::bind can make these "half function calls".

You could use them like this:

// in pEditPunkt
pEditAll(Dyncallback, std::bind(&CPunkt2d::pEditFeldX, this, std::placeholders::_1))

// in pEditAll
WndDyn*   pEditAll(WynDyn_callback& Dyncallback, std::function<WndDyn* (WynDyn_callback)> (func_Call) 
  (WynDyn_callback)) {
    return  func_Call(Dyncallback);
}

CodePudding user response:

@user253751 thank you very much for the effort. So changed this line now return this->pEditFeldX

&(DlgZeile(wi)
                  pEditAll(Dyncallback, this-> 
                  pEditFeldX)//pEditFeldX(AnzSichtChar, WithUnit, 
                  DecConf)
                  FntXUnit(2)
                  pEditFeldY(AnzSichtChar, WithUnit, DecConf)
              );

i'm not getting anything underlined but i'm getting this error again :

Severity Code Description Project File Line Suppression State Error C3867 'Punkt2d::pEditFeldX': non-standard syntax; use '&' to create a pointer to member

something i need to mention the function pEditFeldX belong to the class Punkt2d. Something else i did but did't work is:

WndDyn*   pEditAll(WynDyn_callback& Dyncallback, WndDyn* 
(K_Punkt2d::*func_Call)(WynDyn_callback)) {
    
    return  func_Call(Dyncallback);
}

when i integrate the function pEditAll with defined to which class belong the pointer function, the compiler underline the word 'func_Call' and show this error which is really resonable because func_Call doesn't belong to Punkt2d:

Severity Code Description Project File Line Suppression State Error (active) E0109 expression preceding parentheses of apparent call must have (pointer-to-) function type Geo

I hope I have made myself clear enough and feel free to ask any further questions.

CodePudding user response:

First of all i would like to thanks @user253751 for his patient and great support. i would like to share with you how my code look like now:

#include <functional>
// pointer function
WndDyn*   pEditAll(WynDyn_callback& Dyncallback, std::function<WndDyn* 
            (K_WynDyn_callback)>func_Call) {
    return  func_Call(Dyncallback);
}

//the calle
 WndDyn* K_Punkt2d::pEditPunkt( WndInfo& wi, Int32 AnzSichtChar, 
 Bool WithUnit, const DecimalsConf& DecConf)
{
    WynDyn_callback Dyncallback;
    Dyncallback.AnzSichtChar = AnzSichtChar;
    Dyncallback.WithUnit = WithUnit;
    Dyncallback.DecConf = DecConf;
    
   return &(DlgZeile(wi)
                  pEditAll(Dyncallback, 
     std::bind(&Punkt2d::pEditFeldX, this, 
     std::placeholders::_1))//pEditFeldX(AnzSichtChar, WithUnit, 
      DecConf)
                  FntXUnit(2)
                  pEditFeldY(AnzSichtChar, WithUnit, DecConf)
              );
}
  • Related