打包多个c++对象并传递给函数
pack multiple c++ objects and pass to function
我使用c库进行集成,其中被积函数被声明为fun(…,void*fdata,…)
它使用*fdata指针来传递外部变量,然而,在进行数值积分之前,我需要
使用其他c++库对原始数据进行插值,返回一些插值类对象
基本上,我想把这些对象传递给一个被积函数,它是用户定义的。。。
您可以使用一个结构并将指针传递给它,但在我看来,您没有固定数量的对象要传递,因此动态聚合其他对象会更好地满足您的需求,因此您可以使用std::vector
并将其地址作为func
fdata
参数传递。
一个例子:
#include <vector>
#include <iostream>
using namespace std;
class C //Mock class for your objs
{
public:
C(int x)
{
this->x = x;
}
void show()
{
cout << x << endl;
}
private:
int x;
};
void func(void *fdata) //Your function which will recieve a pointer to your collection (vector)
{
vector <C *> * v = (vector<C *> *)fdata; //Pointer cast
C * po1 = v->at(0);
C * po2 = v->at(1);
po1->show();
po2->show();
}
int main()
{
vector<C *> topass;
topass.push_back(new C(1)); //Create objects and add them to your collection (std::vector)
topass.push_back(new C(2));
func((void *)(&topass)); //Call to func
for(vector<C *>::iterator it = topass.begin(); it != topass.end(); it++)
delete(*it);
}
相关文章:
- STL算法函数在多个一维容器上的使用
- 使用cpp中的结构和函数的多个学生条目
- C++:让函数成为多个类的朋友?
- 从递归函数 C++ 返回多个值
- 使用模板可变参数函数将多个参数传递给另一个函数
- 链接器错误:函数的多个定义
- 重载函数的多个实例
- 从 COM 接口中的函数返回多个值
- C++ 成员函数的多个定义,基于枚举模板参数
- 使用一个回调函数从多个订阅者分配信息
- 函数跨多个C 源文件
- 2 个或更多 TU 中内联函数的多个定义
- C++错误,隐 <function-style-cast> 式要求使用模板化类一次调用多个构造函数的多个转换
- C++结构函数的多个定义
- 如何避免类函数的多个实现
- 使用函数C++返回多个值
- 允许 lambda/回调函数的多个签名作为模板参数
- QT C :函数的多个定义当Header文件包含在MainWindow.h中时
- 编译错误:重载函数的多个实例与 arument 列表匹配
- 函数和多个类层次结构