I was working on a project where I needed to get involved with function pointers, more specifically function pointer to member functions. I have read almost all the related questions, however none of them describing my specific problem. So, I will try to describe my problem by a simple example.
Let's assume that I have three different header files with different classes, like given below:
foo1.h
struct Foo1{ int a1; double b1; void (Foo2::*fp)(const double); } foo2.h
#include "foo1.h" class Foo2{ public: void print_foo2(const double p){ cout << "From Foo2: " << p << endl; } Foo1 foo1; } foo3.h
class Foo3 : public Foo2{ Foo1 foo1; foo1.fp = &Foo2::print_foo2; // cannot do that } So, one of the member variables of struct Foo1 is a function pointer to the member function of Foo2, namely print_foo2. For that reason, I create instance of object foo1 and assign it a pointer to the print_foo2. Class Foo3 inherits from class Foo2.
I noted that, as I thought, may be it can be useful information for solution of the problem. However, I cannot get that pointer to the function as Foo2 is not recognized inside struct Foo1. Even including foo2.h in foo1.h doesn't help. If I am not mistaken, this is because Foo1 requires Foo2 for its member variable during construction, but in the same way Foo2 requires Foo1 to be constructed. So, this creates a deadlock.
Is it the cause of the problem? If yes, how can I solve it?