重铸 *void 函数参数 [英] Recasting *void function arguments

查看:27
本文介绍了重铸 *void 函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前在这里发布了一个问题,如果有人可以帮助我,我想我可以回答与以下内容:

I posted a question here earlier that I think I can answer if someone can help me with the following:

我有一个功能

double func(void* data)

我想传入一个对象或结构体.(在我的情况下是犰狳矩阵,甚至只是 std::vector).

I want to pass in an object or struct. (In my case an armadillo matrix or even just and std::vector).

如何将指向对象的指针作为参数传递给 func(),然后在进入 func() 后,如何将 void 指针重新转换为其原始类型?

编辑:这是最终工作的结果,其中 mat 是犰狳矩阵类:

Edit: Here's what ended up working, where mat is the Armadillo matrix class:

mat A(2,2);
A << 1 << 2 << endr << 3 << 4; // A=[1,2; 3,4]
func(&A);

并在函数中:

double func(void* data) {
   mat* pB = (mat*)(data);
   mat B = pB[0];
}

矩阵 B 和 A 现在包含相同的数据.

The matrix B and A now contain the same data.

推荐答案

如果我理解正确,你需要这样的东西.

If I understand you correctly you need something like this.

double func(void* data_v) {
  struct my_type * data = data_v;
}

func((void*)my_data);

这篇关于重铸 *void 函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆