在C++中不可能声明静态虚函数,也不能将非静态函数强制转换为C样式函数指针.
现在,我有一个简单的ol'C SDK,它大量使用函数指针.
我必须用几个函数指针填充一个结构.我计划使用一个带有一堆静态纯虚方法的抽象类,并在派生类中重新定义它们并用它们填充结构.直到那时我才意识到在C++中不允许使用静态虚拟.
此C SDKs函数签名也没有userData参数.
有什么好的选择吗?我能想到的最好的方法是在每个派生类中定义一些纯虚方法GetFuncA(),GetFuncB(),...和一些静态成员FuncA()/ FuncB(),它们将由GetFuncX()返回.然后抽象类中的函数将调用这些函数来获取指针并填充结构.
编辑 回答John Dibling,能够做到这一点真是太好了:
class Base
{
    FillPointers() { myStruct.funA = myFunA; myStruct.funB = myFunB; ...}
private:
    CStruct myStruct;
    static virtual myFunA(...) = 0;
    static virtual myFunB(...) = 0;
};
class Derived1 : public Base
{
    Derived1() {  FillPointers();  }
    static virtual myFunA(...) {...};
    static virtual myFunB(...) {...};
};
class Derived2 : public Base
{
    Derived2() {  FillPointers();  }
    static virtual myFunA(...) {...};
    static virtual myFunB(...) {...};
};
int main()
{
    Derived1 d1; … 
io_iterator_t enumerator;
kern_return_t   result;
result = IOServiceAddMatchingNotification(
             mNotifyPort,
             kIOMatchedNotification,
             IOServiceMatching( "IOFireWireLocalNode" ),
             serviceMatchingCallback, 
             (void *)0x1234,
             & enumerator );
serviceMatchingCallback((void *)0x1234, enumerator);
如果我将serviceMatchinCallback声明为静态然后它可以工作,但我不希望它是静态的.有没有办法将它传递给非静态回调函数?
谢谢
在C lib中,有一个函数在等待函数指针,这样:
lasvm_kcache_t* lasvm_kcache_create(lasvm_kernel_t kernelfunc, void *closure)
其中lasvm_kernel_t定义为:
typedef double (*lasvm_kernel_t)(int i, int j, void* closure);
现在,如果我将类中定义的方法发送到lasvm_kcache_create:
double cls_lasvm::kernel(int i, int j, void *kparam)
...
lasvm_kcache_t *kcache=lasvm_kcache_create(&kernel, NULL);
我得到:"无法将'double(cls_lasvm :: )(int,int,void)'转换为'double()(int,int,void)'"
我该怎么办?
我在C++应用程序中使用C库.C SDK具有将回调函数指针作为参数的函数.这些功能的签名通常如下:
typedef int (* Func) (type1 c, type2 d);
我的代码是用C++中的类构建的.但是,我不能将任何成员函数作为回调传递给此函数,因为它不接受int (MyClass::*)(type1 c, type2 d)并且只接受int (*)(type1 c, type2 d).
我通过static在各个类中定义所有回调,然后将它们传递给C库然后工作来解决这个问题.
我还是C++的新手,所以我不确定这是否是正确的解决方案?代码有效,但我很想知道我做错了.
想想你的基本GLUT程序.它们只是从main方法运行并包含回调,如`glutMouseFunc(MouseButton),其中MouseButton是方法的名称.
我所做的是将主文件封装到一个类中,这样MouseButton不再是一个静态函数,而是一个实例.但这样做会给我一个编译错误:
错误2错误C3867:'StartHand :: MouseButton':函数调用缺少参数列表; 使用'&StartHand :: MouseButton'创建指向成员c:\ users\angeleyes\documents\visual studio 2008\projects\capstone ver 4\starthand.cpp的指针388 IK Engine
由于类非常庞大,因此无法提供代码示例.
我尝试过使用this->MouseButton但是会出现同样的错误.不能为回调提供指向实例函数的指针吗?
invalid conversion from 'DWORD (*)(void*)' to 'DWORD (*)(void*)'.蛋糕==谎言1 == 0
我不知道这意味着什么......我在这段代码中得到了它
HANDLE CPlugin::CreateWinampThread()    ||
{                                  __VVVVVVVV__
    hWinampThreadHandle = (HANDLE)CreateThread(NULL, 0, StartWinampThread, (void*)this, 0, &dwWinampThreadID);
    if (!hWinampThreadHandle)
        return 0;
     CloseHandle(hWinampThreadHandle);
     return hWinampThreadHandle;
}
.
DWORD  WINAPI CPlugin::StartWinampThread(void* lpParam)[...]
假设我有以下课程:
*.h:
class MyClass{
    void caller();
    int threadProcuder(void *args);
};
*cpp:
void MyClass::caller()
{
    pthread_t i;
    pthread_create(&i,NULL,(void*(*)(void*))&MyClass::threadProcedure,(void*)this);
}
int MyClass::threadProcedure(void *args) 
{
    cout << "body of thread" << endl;
}
不幸的是,线程没有运行.