Passing Pointers of Member Functions OR __cdecl != __thiscal

I’m rewritting some c code into a C++ class for use with ROOT and have run into a problem. I’ve made a short test class which has two member functions in addition to ctor and dtor. One of the functions expects to take a member function pointer.

The code compiles but I can not compile the source code generated by rootcint. When I try to compile I get an error stating that I’m asking for a function pointer of calling convertion __thiscall but sending __cdecl.

The two member functions are…

Int_t classmf::passMe(Int_t var)
{
  return var;
}

Int_t classmf::timesTwo(Int_t (classmf::*func)(Int_t))
{
 return 2 * (*this.*func)(7);
}

And my error is…

I’m running 5.13.04 on a Windows machine with Visual Studio 8.

My compile command is…

I’ve attached the code and header in case more details are needed.

Has anyone run into this problem before or have I just made a coding error?

John Estrada
classmfDict.cpp (29.5 KB)
classmf.h (342 Bytes)
classmf.cpp (515 Bytes)

Hi.

May be I’m wrong and CINT experts know how to fix this, but
IMHO, Cint cannot work correctly with such a code - it generates some nightmarish illegal conversions like this:

static int G__classmfDict_293_0_3(G__value* result7, G__CONST char* funcname, struct G__param* libp, int hash)
{
      G__letint(result7, 105, (long) ((classmf*) G__getstructoffset())->timesTwo((Int_t (*)(Int_t)) G__int(libp->para[0])));//!!!WOW
   return(1 || funcname || hash || result7 || libp) ;
}

Even if you try manually fix this - it’s impossible - pointer-to-member is not a usuall pointer and cannot be cast to/from integer.
May be, you can skip class dictionary generation? Or you can use pointer-to-member as a data-member, but do not pass as a member-function parameter - use it in a function and assign value to it somewhere else.

class classmf
{
public:
   int (classmf::*memPtr)(int);
   void fun()
   {
       (*this.*memPtr)(10);
   }
   int fun1(int);
   int fun2(int);
  .....
};

int main()
{
    classmf obj;
 //////
    obj.memPtr = &classmf::fun1;
    obj.fun();
}

Hi,

try to typedef the pointer-to-member first; for an example (that works :slight_smile: see hist/inc/TFormulaPrimitive.h

Cheers, Axel.

Thanks for the replies. I looked through TFormulaPrimitive but didn’t understand how I could use it for my situation.

I decided to give up and switch over to static functions and that worked. When things slow down, I’ll try the member function approach again and post my solution.

John Estrada