Casting a class method in a constructor

Discussion in 'C++' started by tendots@hotmail.co.uk, Jan 1, 2007.

  1. Guest

    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
     
    , Jan 1, 2007
    #1
    1. Advertising

  2. Lionel B Guest

    On Mon, 01 Jan 2007 14:52:30 -0800, tendots wrote:

    > 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).


    I'm strongly suspect you'll be much better off posting to an OS-specific
    newsgroup:

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.9

    > 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..) { ... }


    Not terribly helpful, because I haven't a clue from your code what LRESULT
    or CALLBACK or WNDPROC or windowClass or ... are.

    > 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.


    But what you _haven't_ made clear is what the cast-from and cast-to
    types actually are. This may well be relevant. BTW the code you post uses
    a C style cast which may be valid C++ but may not behave the same in C++
    as in C.

    > 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


    Fails to run? Fails to compile?

    > and it appears that it fails when the
    > compiler attempts to make the cast.


    Ah. Fails to compile. And the error message is...?

    > 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. )


    Ummm... possibly

    > - is it the compiler?


    Ummm... probably not

    The problems are:

    1) possibly wrong newsgroup, but not really sure, since
    2) insufficient information supplied.

    Please read the FAQ:

    http://www.parashift.com/c -faq-lite/how-to-post.html

    before posting here.

    --
    Lionel B
     
    Lionel B, Jan 2, 2007
    #2
    1. Advertising

  3. Mike Wahler Guest

    <> wrote in message
    news:...
    > 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


    The Microsoft Windows "Window Procedure" function cannot be a nonstatic
    member function. Make it either a static member function or a non-member
    function.

    See the first three items at
    http://www.parashift.com/c -faq-lite/pointers-to-members.html

    They don't discuss your exact issue, but similar ones of the same concept.

    -Mike
     
    Mike Wahler, Jan 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrstephengross
    Replies:
    5
    Views:
    597
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    577
    David Harmon
    Mar 5, 2007
  3. Rahul
    Replies:
    16
    Views:
    1,204
    terminator
    Nov 7, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,243
  5. Wally Barnes
    Replies:
    3
    Views:
    528
    Wally Barnes
    Nov 20, 2008
Loading...

Share This Page