简体   繁体   English

取消引用一个void指针数组

[英]Dereferencing an array of void pointers

Say I have an array of void pointer pointing to objects of different types and I want to pass them through a function and cast them to pointers of their respective type, how would I go about doing this? 假设我有一个指向不同类型对象的void指针数组,我想通过一个函数传递它们并将它们转换为各自类型的指针,我将如何进行此操作? I've tried this current code, but I get error: 我已经尝试过这个当前代码,但是我收到错误:

'void*' is not a pointer-to-object type 'void *'不是指向对象的指针类型

objectA myObjectA;
objectB myObjectB;
objectC myObjectC;
objectD myObjectD;

void *data[4];

data[0] = static_cast<void *>( &myObjectA );
data[1] = static_cast<void *>( &myObjectB );
data[2] = static_cast<void *>( &myObjectC );
data[3] = static_cast<void *>( &myObjectD );

void dereference( void *data )
{
    objectA *objA_ptr = static_cast<objectA *>( data[0] );
    objectB *objB_ptr = static_cast<objectB *>( data[1] );
    objectC *objC_ptr = static_cast<objectC *>( data[2] );
    objectD *objD_ptr = static_cast<objectD *>( data[3] ); 
}

Anyone know the correct way to implement this? 有谁知道实现这个的正确方法?

edit: For context, I'm using opencv and trying to create a simple gui interface with a trackbar. 编辑:对于上下文,我正在使用opencv并尝试使用轨迹栏创建一个简单的gui界面。 Opencv allows for userdata to be fed into the TrackbarCallback which is called when the slider is moved, but only in the form of a void pointer. Opencv允许将userdata输入到TrackbarCallback中,当移动滑块时调用它,但只能以void指针的形式调用。

 int createTrackbar( const string& trackbarname, const string& winname,
                           int* value, int count,
                           TrackbarCallback onChange=0,
                           void* userdata=0); 

Say I have an array of void pointer 假设我有一个void指针数组

void deference( void *data )

That's not an array of void pointers, it's a single void pointer. 这不是一个void指针数组,它是一个单独的void指针。

Change that to void ** , and then since you're mixing C and C++ styles up anyway, why not just use a C style cast? 将其更改为void ** ,然后因为你要混合使用C和C ++样式,为什么不使用C样式转换呢? :

objectA *objA_ptr = (objectA *) data[0];
  1. Derive them all from the same base class. 从同一个基类派生所有这些。

  2. make the destructors virtual if your function needs to delete them. 如果您的函数需要删除它们,则将析构函数设置为虚拟。

  3. make your array BaseClass *data[4] 使你的数组BaseClass *data[4]

  4. make your function void deference(BaseClass **data) 使你的函数void deference(BaseClass **data)

您可以使用reinterpret_cast<>()而不是static_cast<>

The correct function prototype needs to be 需要正确的函数原型

void dereference(void **data)

since you're passing an array of void * , and as we all know in C and C++ an array expression is replaced with a pointer expression in most circumstances. 因为你传递的是一个void *数组,而且我们都知道在C和C ++中,在大多数情况下,数组表达式会被指针表达式替换。

Is there any particular reason why you must use an array of pointers to objects of differing classes? 是否有任何特殊原因必须使用指向不同类对象的指针数组? This smells like a C solution to a C++ problem. 这闻起来像是C ++问题的C解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM