T
tendots
Hi all,
I have come up against a problem when I am registering a callback
within a constructor. The code that I am writing contains a class that
deals with everything to do with bringing up a window in the OS (I'm
just trying to encapsulate everything that is particularly OS specific
in my code).
When the class is created the constructor is called and part of the
goodness of window creation involves registering a callback that deals
with messages passed between the OS and the window. The particular
callback code is a member of this window class.
It all goes something like this ....
class win {
public:
win::win();
LRESULT CALLBACK win::msghandler(arg1,arg2..);
}
win::win()
{
....
windowClass.lpfnWndProc = (WNDPROC)msghandler;
....
}
LRESULT CALLBACK win::msghandler(arg1,arg2..)
{
....
}
Hopefully I have made it clear that the way that the callback member
function is used is that it is cast to another type as part of the
constructor's window initialization.
Now the core of this code has all worked fine and dandy in C for me,
but when I port it to C++ it fails and it appears that it fails when
the compiler attempts to make the cast. I know that you should be able
to call member functions in constructors (and in this case it shouldnt
even be a problem with it not being initialized as it is merely being
registered for later use). The compiler I am using is MSVC 6.0 and (if
you hadnt guessed) its a win 32 system - but I dont think that the OS
is the problem here.
How do I get around the inability to compile?
- is it my lack of knowledge of C++ (I only recently have made the
transition from C . Ouch. )
- is it the compiler?
I dont want to split the constructor up into several functions as this
kind of violates the reasons for using C++ in the first place, and
though I can force it to work by declaring the callback as a static I
create other problems for myself elsewhere.
much thanks to everyone for your help and a happy new year - Mathew
I have come up against a problem when I am registering a callback
within a constructor. The code that I am writing contains a class that
deals with everything to do with bringing up a window in the OS (I'm
just trying to encapsulate everything that is particularly OS specific
in my code).
When the class is created the constructor is called and part of the
goodness of window creation involves registering a callback that deals
with messages passed between the OS and the window. The particular
callback code is a member of this window class.
It all goes something like this ....
class win {
public:
win::win();
LRESULT CALLBACK win::msghandler(arg1,arg2..);
}
win::win()
{
....
windowClass.lpfnWndProc = (WNDPROC)msghandler;
....
}
LRESULT CALLBACK win::msghandler(arg1,arg2..)
{
....
}
Hopefully I have made it clear that the way that the callback member
function is used is that it is cast to another type as part of the
constructor's window initialization.
Now the core of this code has all worked fine and dandy in C for me,
but when I port it to C++ it fails and it appears that it fails when
the compiler attempts to make the cast. I know that you should be able
to call member functions in constructors (and in this case it shouldnt
even be a problem with it not being initialized as it is merely being
registered for later use). The compiler I am using is MSVC 6.0 and (if
you hadnt guessed) its a win 32 system - but I dont think that the OS
is the problem here.
How do I get around the inability to compile?
- is it my lack of knowledge of C++ (I only recently have made the
transition from C . Ouch. )
- is it the compiler?
I dont want to split the constructor up into several functions as this
kind of violates the reasons for using C++ in the first place, and
though I can force it to work by declaring the callback as a static I
create other problems for myself elsewhere.
much thanks to everyone for your help and a happy new year - Mathew