I have a basic question on function pointer.
In the below code snippet, how do I read this ”
*(FARPROC*)&pfn =“?
IFastString *CallCreateFastString(const char *psz) {
static IFastString * (*pfn)(const char *) = 0;
if (!pfn) {
const TCHAR szDll[] = _TEXT("FastString.DLL");
const char szFn[] = "CreateFastString";
HINSTANCE h = LoadLibrary(szDll);
if (h)
*(FARPROC*)&pfn = GetProcAddress(h, szFn);
}
return pfn ? pfn(psz) : 0;
}
This isn’t really about function pointers, but about casting in general.
Suppose
pfnis of typeT. Then&pfnis of typeT*. This gets cast toFARPROC*by the cast expression (the stuff in the parentheses). Finally, this gets dereferenced, yielding aFARPROC&.All in all this just means you’re treating
pfnas if it were of typeFARPROCand assign a value to it.Here’s a generic example: