Hello,
I got a little problem regarding C++ and the use of variables representing functions.
I have the following two classes:
// file: ClassB.h
#ifndef CLASSB_H_INCLUDED
#define CLASSB_H_INCLUDED
// defenition for the function
typedef int (*FUNCTION)(int x, int y);
class ClassB
{
public:
void SetFunction(FUNCTION func);
void DoSomething();
private:
FUNCTION func;
};
#endif // CLASSB_H_INCLUDED
|
// file: ClassA.h
#ifndef CLASSA_H_INCLUDED
#define CLASSA_H_INCLUDED
#include "ClassB.h"
class ClassA
{
public:
ClassA();
TheFunction(int x, int y);
};
#endif // CLASSA_H_INCLUDED
|
// file: ClassB.cpp
#include "ClassB.h"
// sets the function variable
void ClassB::SetFunction(FUNCTION func)
{
this->func = func;
}
// calls our function variable
void ClassB::DoSomething()
{
func(5,5);
}
|
// file: ClassA.cpp
#include "ClassA.h"
ClassA::ClassA()
{
// initialize ClassB and set the function to TheFunction
ClassB classB;
// here is the problem
classB.SetFunction(TheFunction);
classB.DoSomething();
}
// the actual function
int ClassA::TheFunction(int x, int y)
{
return(x + y);
}
|
I hope you understand what I''m trying to do here. ClassB has a variable which contains a function. This variable is assigned with the public function TheFunction of ClassA. The assignment occurs with the method ClassB.SetFunction.
That''s were the compiler gives me an error. It says more or less the following:
cannot convert parameter 1 from void (int x, int y) to void (__cdecl *)(int x, int y)
Can anyone tell me what the problem is? Because when I move the method TheFunction out of ClassA (like this: int TheFunction and not int ClassA::TheFunction) it works perferctly. So the problem must be somewhere that TheFunction is a member function and not a standard function.