#include "stdafx.h"
#include <vector>
#include <iostream>
#include <functional>
#include <vector>
#include <string>
using namespace std;
typedef std::function<double(int, int)> funcIntToDouble;
class parent;
class child
{
public:
double childValue;
child()
{
childValue = 10.5;
}
double InChildFunc(funcIntToDouble f, int x, int i)
{
double d = f(x, i) + childValue;
cout << endl << "InChildFunc: " << d << endl;
return d;
}
};
class parent
{
public:
int y;
child childObject;
vector<double> vec{ 1.1, 2.2, 3.3 };
parent()
{
y = 10.0;
}
double ParentFunc(int x, int i)
{
double d = (double)x * y + vec[i];
return d;
}
void CallChildObject(int parentX, int parentI)
{
funcIntToDouble f = [&](int x, int i) {
double d = ParentFunc(x, i);
return d;
};
double r = childObject.InChildFunc(f, parentX, parentI);
cout << endl << "CallChildObject: " << r << endl;
}
};
int main()
{
parent p;
p.CallChildObject(10, 1);
getchar();
return 0;
}
20 November 2018
C++ pass member function pointer as function parameter example
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment