#include "stdafx.h"
#include <iostream>
#include <functional>
using namespace std;
class Point
{
public:
Point(double _x, double _y) :x(_x), y(_y) {};
double x;
double y;
};
typedef std::function<void*(void*)> genericFunc;
void * func1(void * argv) {
double * d = (double *)argv;
double x = d[0];
double y = d[1];
Point p(x, y);
Point* pp = &p;
return (void *)pp;
}
void * func2(void * argv) {
double * d = (double *)argv;
double x = d[0];
double y = d[1];
double z = d[2];
double R = x * y * z;
return (void *)&R;
}
void * TestFunc(genericFunc f, void* p)
{
return f(p);
}
int main()
{
double argv[]{ 1.2, 2.3, 3.4 };
Point p = * ( (Point*) TestFunc(&func1, (void*)argv) );
double d = *((double*)TestFunc(&func2, (void*)argv));
cout << "p:" << p.x << "," << p.y << ", d:" << d << endl;
getchar();
return 0;
}
20 November 2018
C++ generic function pointer example / use (void*) as object pointer
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment