0

I have a simple code

class a{ public: double (a::*fun)(const double &) const; // .... // }; class b{ public: a c1; double f(const double & x) const{ return 0; } b(){ c1.fun = f; } }; 

It mean class "a" must work with functions of a certain type, and his work does not depend of this function realization (for example algebraic interpolation). I want make parameter of class - function. But I have some problem. Compiler write

a value of type "int (b::)(const int &x) const" cannot be assigned to an entity of type "int (a::)(const int &) const"

How it's made correct?

2 Answers 2

1

double (a::*fun)(const double &) const declares a pointer to a member function of class a which return double and takes in const double&.

In your example you are trying to assign the function defined in class b. that's why you get the error.

Sign up to request clarification or add additional context in comments.

Comments

1

Perhaps a template would solve this:

template <typename T> struct Params { double (T::* fun)(const double &) const; // ... }; class B { Params<B> params; double f(const double &) const; public: B() : params(&B::f) { } // ... }; 

To invoke the function somewhere in a non-static member function of B on the current instance you would say:

double d; (this->*params.fun)(d); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.