0

I am using a c library to do integration, where the integrand is declared as fun(...,void *fdata,...)

which uses *fdata pointer to pass external variables, however, before doing numerical integration, I need to

interpolate the raw data using other c++ libraries, giving back some interpolating class objects,

basically I want to pass these objects to a integrand which is user-defined ...

lorniper
  • 626
  • 1
  • 7
  • 29
  • 2
    You should clarify what you are trying to do. Details matter. – juanchopanza Sep 09 '14 at 06:10
  • juanchopanza's comment is valid - to give you a better idea of what's relevant, you might tell us whether you are trying to pass many objects of the same type? Why you're using `void*`s and not templating the function parameters or using pointers to a base class. Why did you put the "C" tag on the question? – Tony Delroy Sep 09 '14 at 07:00
  • Please do not deface your question in this manner. That is *completely unfair* to the user who spent time answering below. – Andrew Barber Sep 16 '14 at 07:36

1 Answers1

1

You could use an structure and pass a pointer to it but it seems to me that you don't have a fixed number of objects to pass and therefore that an object aggregating others dynamically would suit better your needs so you can use a std::vector and pass its address as func fdata parameter.

An example:

#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);
}