What I want to do is:
class A
{
public:
double sum(double a, double b);
double max(double a, double b);
}
template <typename T>
class B
{
std::vector<T> data;
public:
double sum (double a, double b);
double max (double a, double b);
double average( MyFunction, double a, dobule b)
{
double sum = 0;
int n = data.size();
for ( int i = 0; i < n; i++)
sum = sum + data[i].MyFunction(double a, double b)
return sum / n;
}
}
example:
double average( max, double a, double b)
{
double sum = 0;
int n = data.size();
for ( int i = 0; i < n; i++)
sum = sum + data[i].max(double a, double b)
return sum / n;
}
Why?
B< B< A> >
worksWhat have I tried?
S1:
typedef double (A::*MyFunctionf)(double, double);
typedef double (B<A>::*MyFunctionff)(double, double);
typedef double (B<B<A> >::*MyFunctionfff)(double, double);
S2 (based on Template typedefs - What's your work around?):
template <typename rtype, typename t>
struct CallTemplate
{
typedef rtype (t::*ptr)(double, double);
};
// the function becomes :
template <typename T>
template<typename rtype>
double B<T>::average(CallTemplate<double, T> MyFunction, double a, double b)
{
double sum = 0;
int n = data.size();
for ( int i = 0; i < n; i++)
sum = sum + (data[i].*MyFunction)( a, b)
return sum / n;
}
example:
// makes an error "Myfunction was not declared" + "
// dependent-name'{anonymous}::CallTemplate<double, A>::ptr'
// is parsed as a non-type, but instantiation yields a type"
CallTemplate<double, A>::ptr MyFunction = &A::max;
Average(max, t, v);
I do not know where the problem comes from. I have also tried Boost.Function
Functions can be passed into other functionsFunctions, like any other object, can be passed as an argument to another function. >>> def greet(name="world"): ... """Greet a person (or the whole world by default).""" ... print(f"Hello {name}!") ... >>> greet("Trey") Hello Trey!
There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method.
A parameter is a named variable passed into a function. Parameter variables are used to import arguments into functions. For example: function example(parameter) { console.
Yes it's possibile.
You're looking for member pointers. The syntax is not obvious however:
struct A
{
double x, y;
A(double x, double y) : x(x), y(y) {}
double sum() { return x + y; }
double max() { return std::max(x, y); }
};
This is a class that defines a couple of methods (sum
and max
).
template <typename T>
struct B
{
std::vector<T> data;
double average(double (T::*MyMethod)())
{
double sum = 0;
int n = data.size();
for (int i = 0; i < n; i++)
sum = sum + (data[i].*MyMethod)();
return sum / n;
}
};
This is a class with a method that accepts a method pointer and that will compute the average of the result of calling the pointed method over the elements of a vector.
An example of passing the two A
methods is:
int main(int argc, const char *argv[]) {
B<A> b;
b.data.push_back(A(1, 2));
b.data.push_back(A(3, 4));
b.data.push_back(A(5, 6));
std::cout << b.average(&A::max) << std::endl;
std::cout << b.average(&A::sum) << std::endl;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With