C++ 当我调用指针成员函数时,参数的混合值

C++ 当我调用指针成员函数时,参数的混合值,c++,function,pointers,qt-creator,C++,Function,Pointers,Qt Creator,我从另一个类调用了成员函数的指针。此函数有2个参数。 它正在工作。。。但是在这个成员函数中,在调试器中,我看到参数的值被交换(混合)。我看到了正确的值,但在正确的变量(参数)中看不到。可能是虫子。。。 Qt创建者c++ void SmartCTL::OnReadBufferA(int shm_id, int length); //declaration hookedIDs[HOOKIDPOINTERS::READBUFFERA] = (void *)&SmartCTL::OnReadB

我从另一个类调用了成员函数的指针。此函数有2个参数。 它正在工作。。。但是在这个成员函数中,在调试器中,我看到参数的值被交换(混合)。我看到了正确的值,但在正确的变量(参数)中看不到。可能是虫子。。。 Qt创建者c++

void SmartCTL::OnReadBufferA(int shm_id, int length); //declaration
 hookedIDs[HOOKIDPOINTERS::READBUFFERA] = (void *)&SmartCTL::OnReadBufferA; // Save pointer of function
memcpy(hookedIDs[smartCTL.shellID],smartCTL.hookedIDs,8*sizeof(void *));// copy pointers
void (*pf)(int,int )= (void(*)( int,int ))hookedIDs[i][HOOKIDPOINTERS::READBUFFERA];// set pointer 
 pf(shells[i]->readBuff.shm_id,shells[i]->readBuff.length); // call hear

结果我得到了长度为hells[I]>readBuff.shm_id的值和shm_id为value shell[I]>readBuff.length的值,这不是一个bug。你所拥有的是未定义的行为。成员函数与常规函数不同。成员函数中有一个
this
,其获取方式是通过类类型的隐式函数参数。所以

void SmartCTL::OnReadBufferA(int shm_id, int length)
真的有点像

void SmartCTL::OnReadBufferA(SmartCTL& this_ref, int shm_id, int length)
std::vector<std::function<void(int, int)>> functions;
functions.push_back(some_regular_function);
functions.push_back([](int shm_id, int length){ SmartCTL s; return s(shm_id, length); });
这就是为什么不能将指向成员函数的指针强制转换为指向常规函数的指针

如果您需要在“数组”中同时包含成员函数和常规函数,那么您将需要一个。它使用类型擦除来允许您存储具有相同接口的不同类型的函数对象。例如,你可以有类似

void SmartCTL::OnReadBufferA(SmartCTL& this_ref, int shm_id, int length)
std::vector<std::function<void(int, int)>> functions;
functions.push_back(some_regular_function);
functions.push_back([](int shm_id, int length){ SmartCTL s; return s(shm_id, length); });
std::向量函数;
功能。推回(一些常规功能);
函数.push_-back([](int-shm_-id,int-length){SmartCTL s;返回s(shm_-id,length);});

另一种选择是使成员函数成为静态函数。静态函数没有绑定类的实例,因此可以将其视为常规函数。

为什么要执行这么多强制转换?特别是C型演员?这看起来真的很可疑,如果您在其中的某个地方调用未定义的行为,我也不会感到惊讶