The code below is running correctly with any online gcc compiler I found (gcc 9.2.0), it also run correctly with CYGWIN gcc compiler, but unfortunately it doesn't work well with MINGW gcc compiler - looks like it passes invalid parameter as "this" to "methodA" and "methodB" methods, when they are called, instead of expected results (56,58) i get some random high numbers.
#include <iostream>
using namespace std;
class CallbackBase
{
public:
using METHOD_TYPE = int (CallbackBase::*)(...);
};
class CallbackProvider : public CallbackBase
{
public:
int methodA(int a,int b,int c)
{
return a+b+c+d;
}
int methodB(double a,double b,double c)
{
return a+b+c+d;
}
private:
int d=8;
};
class CallbackRunner
{
public:
CallbackBase::METHOD_TYPE m_method;
CallbackBase* m_this;
void install (CallbackBase* _this, CallbackBase::METHOD_TYPE _method)
{
m_method =_method;
m_this =_this;
}
int Run1()
{
return (m_this->*m_method)(15L,16L,17L);
}
int Run2()
{
return (m_this->*m_method)(15.6,16.7,17.8);
}
};
int main()
{
CallbackProvider cp;
CallbackRunner cr;
cr.install(&cp,(CallbackBase::METHOD_TYPE)&CallbackProvider::methodA);
cout << "result " << cr.Run1() << endl;
cr.install(&cp,(CallbackBase::METHOD_TYPE)&CallbackProvider::methodB);
cout << "result " << cr.Run2() << endl;
return 0;
}
The problem is solved if I add __cdecl attribute to this methods:
int __cdecl methodA(int a,int b,int c)
int __cdecl methodB(double a,double b,double c)
I doesn't use -mrtd compilation flag. According to this, __cdecl should be a default calling convention for gcc compilers but looks like it doesn't the case for MINGW.
Is that possible to set __cdecl as a default calling convention for my project? or as alternative, is there a way to set "default" attribute to all the methods?
I am using Windows 10 with 64 bit architecture.