jeudi 2 décembre 2021

How to initialize static member function pointer variable

We have a legacy class used in production something like below to handle UI events:

//.H file
class A
{
public:
static IntPtr DD_STDCALL DispatchMsg(HWND hwnd, UInt umsg, WPARAM wparam, LPARAM lparam);
..other members...
};

//.CPP file
IntPtr DD_STDCALL A::DispatchMsg(HWND hwnd, UInt umsg, WPARAM wparam, LPARAM lparam)
{
....
}

Now actually I want to implement something like this in above function:

IntPtr DD_STDCALL A::DispatchMsg(HWND hwnd, UInt umsg, WPARAM wparam, LPARAM lparam)
{
    if(selfdlgproc)
    {
        return form->selfdlgproc(umsg, wparam, lparam);
    }
....
}

Where selfdlgproc is a function pointer that will have a user defined function address that processes events.

So I modified the class declation like below:

class A
{
....
    // dispatches messages to Form subclasses
    typedef IntPtr(* DialogProc)(UInt umsg, WPARAM wparam, LPARAM lparam);
    static IntPtr DD_STDCALL DispatchMsg(HWND hwnd, UInt umsg, WPARAM wparam, LPARAM lparam);
    void SetDialogProc(DialogProc dlgproc);
    static DialogProc selfdlgproc;
};

The objective is to have a function pointer assigned a value during execution - if the same is not null then call user defined function otherwise the default behaviour. So:

void A::SetDialogProc(DialogProc dlgproc)
{
    selfdlgproc = dlgproc;
}

IntPtr DD_STDCALL A::DispatchMsg(HWND hwnd, UInt umsg, WPARAM wparam, LPARAM lparam)
{
    if(selfdlgproc)
    {
        return A->selfdlgproc(umsg, wparam, lparam);
    }
}

But how can I initialize the static member funrction pointer variable:

IntPtr A::selfdlgproc = nullptr;

During compilation time it states incompatible type? How can I initlaize a static function pointer of a class and implment the above logic?

Aucun commentaire:

Enregistrer un commentaire