传递对指向 void 类型的指针的指针的引用
Passing a reference to a pointer to pointer of type void
我有一个接受对void**的引用的函数。
bool boExecute(void**& vOutParameter);
我试图在 vOutParameter 中写入一些值,但是当我在 main(( 中检查它时,该值没有写入。
在这种情况下,什么和参考?它是对指针的引用还是对指向指针的指针的引用?
在 boExecute 中,我像这样添加它:
bool boExecute(void**& vOutParameter)
{
Struct_Type* Out = new Struct_Type[4];
for (int i=0; i<4; ++i)
{
memcpy(&(Out[i]), Referenced_Struct[i], sizeof(Struct_Type));
}
*vOutParameter = reinterpret_cast<void*>Out;
Out = null;
return true;
}
Referenced_Struct的类型为 Struct_Type**,它有两个成员,int32_val 和 int64_val。
主要内容:
void main(void)
{
void **test;
boExecute(test);
Struct_Type** temp = reinterpret_cast<Struct_Type**>(test);
Struct_Type* temp1 = *temp;
for (int i=0; i<4; ++i)
{
printf("%d, %d", temp1[i].int32_val, temp1[i].int64_val);
}
}
我正在做的事情有问题吗?当我更改 *vOutParameter 时,*vOutParameter 的内容应该在函数外更新,对吧?
我正在做的事情有问题吗?
您应该实际使用 C++ 重写函数,而不是使用奇怪的 C 语义,为错误和 out 参数提供不必要的布尔返回值:
template<typename It>
std::vector<Struct_type> boExecute(It Reference_begin, It Reference_end)
{
std::vector<Struct_type> Out;
std::copy(Reference_begin, Reference_end, std::back_inserter(Out));
return Out;
}
现场演示
请注意,由于 RVO(返回值优化(,返回整个向量时没有性能问题。所以你可以睡觉,知道你的记忆是安全的。
在这种情况下,什么和参考?它是对指针的引用还是对指向指针的指针的引用?
一般来说,T&
是对T
的引用。这意味着void**&
是对void**
的引用,而是指向void
的指针。
相关文章:
- C++中的双指针类型转换
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 将类指针类型转换为键时出错
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- C++在一个映射中存储不同的指针类型(并处理销毁)
- 指针类型类成员的动态强制转换的恒定性是什么?
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- STL 函数和函数类型与函数指针类型
- 如何调用指针类型的方法(禁用多态性)?
- 为什么新表达式可以正确生成指针类型,即使它应该返回 void*?
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 是否允许调用方对我的 Builder 类使用任何指针类型(包括智能指针)?
- OPENCL 警告:不兼容的指针类型将'float __global[16]'传递给类型为 '__global float4 的参数 *
- 专门用于"direct"函数类型(与函数指针类型相对)
- 指向成员的指针类型和模板
- 返回对常量结构(指针类型)成员的引用:明显的左值到右值转换
- 在C++17中,为什么类模板和函数模板的指针类型推导明显不一致
- 为什么允许将整型、枚举和指向成员的指针类型reinterpret_cast到自身?