(void*) casting- what is this used for?

余生长醉 提交于 2020-01-01 03:20:07

问题


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

I have seen (void*) being used to cast, usually to function calls. What is this used for?


回答1:


void*, usually referred to as a void pointer, is a generic pointer type that can point to an object of any type. Pointers to different types of objects are pretty much the same in memory and so you can use void pointers to avoid type checking, which would be useful when writing functions that handle multiple data types.

Void pointers are more useful with C than C++. You should normally avoid using void pointers and use function overloading or templates instead. Type checking is a good thing!




回答2:


A void pointer is a pointer to a value whose type and size are unknown. I'll give an example of how it might be used in C - although other commenters are correct in saying that it by and large shouldn't be used (and, in fact, isn't necessary) in C++.

Suppose you have a struct which stores a key and a value. It might be defined like this:

typedef struct item_t
{
    char *key;
    int value;
} item_t;

So you could use this structure to match up any string key with an integer value. But what if you want to store some arbitrary value? Maybe you want some keys to match up with integers, and some keys to match up with doubles? You need to define your struct in such a way that it will keep track of (with a pointer) a value of arbitrary type and size. The void * fits the bill in this case:

typedef struct item_t
{
    char *key;
    void *value;
} item_t;

Now, if your value is an int, you can get at its value using (int *) value, or if its value is a double, you can do it using (double *) value. This assumes, of course, that the code using the struct knows what type it expects to see in what context.

In C++, though, this same functionality is usually achieved not through the use of void *, but by using things like templates or a superclass which all of your different types descend from (like Object in Java, for example - a list of Objects can store some String's, some Integer's, etc.).




回答3:


It's the most generic way of providing a pointer to an object in C/C++. Like object in C# or Java




回答4:


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

Eg in your generic code you would pass the void* as a parameter, 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;
}


来源:https://stackoverflow.com/questions/16866333/void-casting-what-is-this-used-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!