Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将对象指针强制转换为双空心指针(指向空心指针的指针)_C++_Pointers_Casting_Type Conversion_Pointer To Pointer - Fatal编程技术网

C++ 将对象指针强制转换为双空心指针(指向空心指针的指针)

C++ 将对象指针强制转换为双空心指针(指向空心指针的指针),c++,pointers,casting,type-conversion,pointer-to-pointer,C++,Pointers,Casting,Type Conversion,Pointer To Pointer,我在一个库中有一个类方法(无法更改),它将一个双空指针作为其参数之一。声明如下: bool Queue::pop(void **packet, int &size, unsigned short &sn) 在我的应用程序代码中,我想向这个函数传递一个指向另一种对象的指针,在本例中是指针类型。如果我像下面的代码片段一样将指针转换为(void**),似乎可以做到这一点而不会出现任何编译器错误,但我怀疑这是否会导致正确的行为。这样转换指针有效吗 guint16 sn; int siz

我在一个库中有一个类方法(无法更改),它将一个双空指针作为其参数之一。声明如下:

bool Queue::pop(void **packet, int &size, unsigned short &sn)
在我的应用程序代码中,我想向这个函数传递一个指向另一种对象的指针,在本例中是指针类型。如果我像下面的代码片段一样将指针转换为
(void**)
,似乎可以做到这一点而不会出现任何编译器错误,但我怀疑这是否会导致正确的行为。这样转换指针有效吗

guint16 sn;
int size;
GstBuffer *buf; 
Queue *Q = ... // create a Queue instance
Q->pop((void **)&buf, size, sn);  // is this conversion valid?
size = gst_buffer_get_size(buf);

出于所有目的,
void
指针可以保存任何对象(数据类型)的地址,它可以指向任何对象,并且可以对任何对象进行类型转换,您的代码是有效的,我只会使用更惯用的转换:

Q->pop(reinterpret_cast<void**>(&buf), size, sn);
输出:

0xb83eb0
0x7ffc181ab2c8
0xb83eb0
0x7ffc181ab2c8
20

显式强制转换甚至只是需要的,因为它是指向指针的指针,否则转换将是隐式的,不需要强制转换。

@PluginPenguin你完全正确,智能指针更好,但出于这个答案的目的,我觉得不需要使用它,尽管值得一提。
0xb83eb0
0x7ffc181ab2c8
0xb83eb0
0x7ffc181ab2c8
20