How To Use Function Pointer in Class

Gentlemen,
how to Pass a Pointer to a Function in a Class
i am unable to Pass the Address Of the WndProc() Function
to the WndClass{}; Structure as is required for any
window creation.i have tried typecasting and everyting
thing else i could think of last thing is maybe first make a
object of the class and then call its or use its functions or variables
but i am doing all this while defining a class itself.and all of this has to be done
inside the constructor if i will declare a object inside constructor that would result
in an endless loop of class instance creations.

what Should i write for

EditorAppMainWndClassEx.lpfnWndProc = ?

i am sending below the gist of MainWindowCreation Class
where this thing is happening.

ERRORS this is one kind of error when i tried many things to set it right
i hope you know exactly what needs to be done.Speaking Generally it is always
putting up some Sort of a TypeCast Violation Error in various Flavours

[code]
Compiling...
MainWindowCreation.cpp
mainwindowcreation.h(39) : error C2440: 'initializing' : cannot convert from '' to 'long (__cdecl *)(struct HWND__ *,unsigned int,unsigned int,long)'
None of the functions with this name in scope match the target type
mainwindowcreation.h(46) : error C2440: '=' : cannot convert from 'long (__cdecl *)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,uns
igned int,unsigned int,long)'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.
MainWindowCreation.obj - 2 error(s), 0 warning(s)
[/code]

File:- MainWindowCreation.H
[code]
class MainWindowCreation
{
public:

WNDCLASSEX EditorAppMainWndClassEx ;
HINSTANCE hInstanceEditorAppMain ;
HINSTANCE hPrevInstanceEditorAppMain ;
LPSTR lpCmdLineEditorAppMain ;
int nCmdShowEditorAppMain ;

ATOM MyRegisterClass ( HINSTANCE );
BOOL InitInstance ( HINSTANCE, int );
long EditorAppMainMessageQue ( HWND, UINT , WPARAM, LPARAM );
LRESULT CALLBACK EditorAppMainWndProc ( HWND, UINT , WPARAM, LPARAM ); // UINT Is Message
LRESULT CALLBACK About ( HWND, UINT , WPARAM, LPARAM );


int WINAPI WinMain( HINSTANCE hInstanceEditorAppMain ,
HINSTANCE hPrevInstanceEditorAppMain ,
LPSTR lpCmdLineEditorAppMain ,
int nCmdShowEditorAppMain );



MainWindowCreation();
virtual ~MainWindowCreation();

};
[/code]
File:- MainWindowCreation.cpp
[code]

int WINAPI MainWindowCreation::WinMain
(
HINSTANCE hInstanceEditorAppMain,
HINSTANCE hPrevInstanceEditorAppMain,
LPSTR lpCmdLineEditorAppMain ,
int nCmdShowEditorAppMain
)
{

TCHAR EditorAppMainszClassName[]="EditorAppMainClass";

// long ( * WndProcPointer )( HWND, UINT, WPARAM, LPARAM ) = EditorAppMainWndProc ;
// i tried this to see if Pointer would work here it does not

EditorAppMainWndClassEx.cbSize = sizeof(WNDCLASSEX);
EditorAppMainWndClassEx.hbrBackground = NULL;
EditorAppMainWndClassEx.hCursor = NULL;
EditorAppMainWndClassEx.hIcon = NULL;
EditorAppMainWndClassEx.hIconSm = NULL;
EditorAppMainWndClassEx.hInstance = hInstanceEditorAppMain ;
EditorAppMainWndClassEx.lpfnWndProc = MainWindowCreation::EditorAppMainWndProc ; //this is not working it gives error i have tried prefixing and amending in many ways
EditorAppMainWndClassEx.lpszClassName = EditorAppMainszClassName ;
EditorAppMainWndClassEx.lpszMenuName = NULL;
EditorAppMainWndClassEx.style = NULL;
EditorAppMainWndClassEx.cbClsExtra = 0 ;
EditorAppMainWndClassEx.cbWndExtra = 0 ;

return 0;
}
[/code]

Should not the Things Declared Public in a Class Be assecible to all the
Functions of the Class itself while we are writing the code for those
functions. It Should "MUST"!!

Comments

  • [b][red]This message was edited by MT2002 at 2005-11-19 18:12:25[/red][/b][hr]
    [b][red]This message was edited by MT2002 at 2005-11-19 17:59:5[/red][/b][hr]
    : Gentlemen,
    : how to Pass a Pointer to a Function in a Class
    : i am unable to Pass the Address Of the WndProc() Function
    : to the WndClass{}; Structure as is required for any
    : window creation.i have tried typecasting and everyting
    : thing else i could think of last thing is maybe first make a
    : object of the class and then call its or use its functions or variables
    : but i am doing all this while defining a class itself.and all of this has to be done
    : inside the constructor if i will declare a object inside constructor that would result
    : in an endless loop of class instance creations.
    :
    : what Should i write for
    :
    : EditorAppMainWndClassEx.lpfnWndProc = ?
    :
    : i am sending below the gist of MainWindowCreation Class
    : where this thing is happening.
    :
    : ERRORS this is one kind of error when i tried many things to set it right
    : i hope you know exactly what needs to be done.Speaking Generally it is always
    : putting up some Sort of a TypeCast Violation Error in various Flavours
    :
    : [code]
    : Compiling...
    : MainWindowCreation.cpp
    : mainwindowcreation.h(39) : error C2440: 'initializing' : cannot convert from '' to 'long (__cdecl *)(struct HWND__ *,unsigned int,unsigned int,long)'
    : None of the functions with this name in scope match the target type
    : mainwindowcreation.h(46) : error C2440: '=' : cannot convert from 'long (__cdecl *)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,uns
    : igned int,unsigned int,long)'
    : This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    : Error executing cl.exe.
    : MainWindowCreation.obj - 2 error(s), 0 warning(s)
    : [/code]
    :
    : File:- MainWindowCreation.H
    : [code]
    : class MainWindowCreation
    : {
    : public:
    :
    : WNDCLASSEX EditorAppMainWndClassEx ;
    : HINSTANCE hInstanceEditorAppMain ;
    : HINSTANCE hPrevInstanceEditorAppMain ;
    : LPSTR lpCmdLineEditorAppMain ;
    : int nCmdShowEditorAppMain ;
    :
    : ATOM MyRegisterClass ( HINSTANCE );
    : BOOL InitInstance ( HINSTANCE, int );
    : long EditorAppMainMessageQue ( HWND, UINT , WPARAM, LPARAM );
    : LRESULT CALLBACK EditorAppMainWndProc ( HWND, UINT , WPARAM, LPARAM ); // UINT Is Message
    : LRESULT CALLBACK About ( HWND, UINT , WPARAM, LPARAM );
    :
    :
    : int WINAPI WinMain( HINSTANCE hInstanceEditorAppMain ,
    : HINSTANCE hPrevInstanceEditorAppMain ,
    : LPSTR lpCmdLineEditorAppMain ,
    : int nCmdShowEditorAppMain );
    :
    :
    :
    : MainWindowCreation();
    : virtual ~MainWindowCreation();
    :
    : };
    : [/code]
    : File:- MainWindowCreation.cpp
    : [code]
    :
    : int WINAPI MainWindowCreation::WinMain
    : (
    : HINSTANCE hInstanceEditorAppMain,
    : HINSTANCE hPrevInstanceEditorAppMain,
    : LPSTR lpCmdLineEditorAppMain ,
    : int nCmdShowEditorAppMain
    : )
    : {
    :
    : TCHAR EditorAppMainszClassName[]="EditorAppMainClass";
    :
    : // long ( * WndProcPointer )( HWND, UINT, WPARAM, LPARAM ) = EditorAppMainWndProc ;
    : // i tried this to see if Pointer would work here it does not
    :
    : EditorAppMainWndClassEx.cbSize = sizeof(WNDCLASSEX);
    : EditorAppMainWndClassEx.hbrBackground = NULL;
    : EditorAppMainWndClassEx.hCursor = NULL;
    : EditorAppMainWndClassEx.hIcon = NULL;
    : EditorAppMainWndClassEx.hIconSm = NULL;
    : EditorAppMainWndClassEx.hInstance = hInstanceEditorAppMain ;
    : EditorAppMainWndClassEx.lpfnWndProc = MainWindowCreation::EditorAppMainWndProc ; //this is not working it gives error i have tried prefixing and amending in many ways
    : EditorAppMainWndClassEx.lpszClassName = EditorAppMainszClassName ;
    : EditorAppMainWndClassEx.lpszMenuName = NULL;
    : EditorAppMainWndClassEx.style = NULL;
    : EditorAppMainWndClassEx.cbClsExtra = 0 ;
    : EditorAppMainWndClassEx.cbWndExtra = 0 ;
    :
    : return 0;
    : }
    : [/code]
    :
    : Should not the Things Declared Public in a Class Be assecible to all the
    : Functions of the Class itself while we are writing the code for those
    : functions. It Should "MUST"!!
    :
    [blue]
    The window proc has the _cdecl calling convention (for global
    C routines)--which is a different convention then C++ member
    functions. You *must* declare it static..[/blue][code]
    [blue]//"CALLBACK" is simply defined as _cdecl[/blue]
    [b]static[/b] LRESULT CALLBACK EditorAppMainWndProc ( HWND, UINT , WPARAM, LPARAM ); // UINT Is Message

    //...

    int WINAPI MainWindowCreation::WinMain (...)
    {

    [blue] // Note the CALLBACK naming convention[/blue]
    long ([b]CALLBACK[/b] *WndProcPointer ) ( HWND, UINT, WPARAM, LPARAM ) = EditorAppMainWndProc ;

    //etc..
    [/code]


  • [blue]
    The window proc has the _cdecl calling convention (for global
    C routines)--which is a different convention then C++ member
    functions. You *must* declare it static..[/blue][code]
    [blue]//"CALLBACK" is simply defined as _cdecl[/blue]
    [b]static[/b] LRESULT CALLBACK EditorAppMainWndProc ( HWND, UINT , WPARAM, LPARAM ); // UINT Is Message

    //...

    int WINAPI MainWindowCreation::WinMain (...)
    {

    [blue] // Note the CALLBACK naming convention[/blue]
    long ([b]CALLBACK[/b] *WndProcPointer ) ( HWND, UINT, WPARAM, LPARAM ) = EditorAppMainWndProc ;

    //etc..
    [/code]

    Mr. Sir MT2002

    Thankyou
    Thankyou Very Much
    your Point has been understood and implemented
    and the error is gone.i am back to C++ after a Long
    Detour to c i am sure i will forget everything about c when i return
    to it from c++.and hence the endless loop of learning.

    I wish to trouble you more in future. :-)



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories