What is (void*) casting used for?

前端 未结 4 1868
有刺的猬
有刺的猬 2021-02-09 12:11

I did try searching for this on Stack Overflow, but I think due to the syntax and not knowing exactly what to search I became a little unstuck.

I have seen (v

4条回答
  •  遇见更好的自我
    2021-02-09 13:01

    That is mostly used when you want to pass a parameter that can have different states.

    For example, in your generic code you would pass the void* as a parameter, and then in the "specific" implementation of that code, you would cast the void* to whatever you want

    Generic code, abc.h

    void Test(void* pHandle);
    

    Windows code, abc.cpp

    void Test(void* phandle)
    {
       WindowsStructure* myStruct = (WindowsStructure*)(pHandle);
        myStruct->getWhatINeed;
    }
    

    Linux code, abc.cpp

    void Test(void* phandle)
    {
       LinuxStructure* myStruct = (LinuxStructure*)(pHandle);
        myStruct->getWhatINeed;
    }
    

提交回复
热议问题