Refer to this small top-of-my-head coed snippet:
typedef void __fastcall (__closure *GetHTTPCallBack)(String filename);
class Foo {
private:
public:
GetHTTPCallBack Callback;
__fastcall Foo (void) { Callback = NULL; }
__fastcall Foo (GetHTTPCallBack callback) { Callback = callback; }
};
class Bar {
private:
Foo *foo;
public:
void __fastcall CallbackFunction(String fileName) { // Do something }
void __fastcall SetByConstructor(void) { foo = new Foo(CallbackFunction); }
void __fastcall SetByAssignment (void) { foo = new Foo(); foo->Callback = CallbackFunction; }
};
Bar bar;
Now to the problem.
If Call bar.SetByAssignment()
it works fine, the function pointer is set and called correctly from Foo. But in this case it is exposed as a public variable. I would like it to be private.
I would like to simplify the class, and hide it to be private, by passing the function pointer in the constructor, bar.SetByConstructor()
but here I get compiler error:
[bcc32 Error] : Member function must be called or its address taken
I thought the __closure
would make this possible. Is it possible at all or I'm I just doing it wrong?
Do what the compiler tells you. Pass the memory address of the method. How do you get the memory address of anything? Use the &
address operator:
foo = new Foo(&CallbackFunction);
It is good habit to do that when using an assignment as well (though in that case it is optional):
foo->Callback = &CallbackFunction;