Slimelite BBS

C++ Problems

Back to EMULATE

From Jerry Coffin · 1996-04-12 19:22 · #73712 in EMULATE

On (08 Apr 96) Paul Johnston wrote to All...

 PJ> Ok, I've got a class 'Message' and a class 'WriteMsg' derived from
 PJ> this. Now, I want to overload the '=' operator, for Message = WriteMsg
 PJ> But I can't, cause I need to have declared WriteMsg before I can use
 PJ> it in the Message class, but I can't declare WriteMsg before I have
 PJ> declared the Message class. Help :-)

class WriteMsg;

class Message {

public:
    Message &operator=(const WriteMsg &other) {
        // whatever
    }
};

class WriteMsg: public Message {

    // ...
};

 PJ> This problem is Windows specific. I have a function (*not* in any
 PJ> class):

 PJ> BOOL CALLBACK ReadMsgProc(HWND hDlg, UINT message, WPARAM wParam,
 PJ> LPARAM lParam);

 PJ> It is used as a dialog box callback function, and works just fine.
 PJ> Now, I want to have that function as a member of a class. I do it, but
 PJ> the following line:

 PJ> DlgProc=(FARPROC) &ReadMsg::ReadMsgProc;

 PJ> brings up a 'cannot cast' error. ie: you can cast the C function like
 PJ> that, but *not* the C++ function. What am I doing wrong?

To do this, you have to make the call-back function static.  However,
you generally want it to be virtual.  The workaround looks something
like this:

class WndBase {
    static BOOL CALLBACK WndProc(HWND hDlg, UINT message,
        WPARAM wParam, LPARAM lParam)
    {
        WndBase *Cl = (WindowBase *)GetWindowLong(hDlg, MY_OFFSET);
        return Cl->MsgProc(hDlg, message, wParam, lParam);
    }
public:
    CreateAWindow( /* ... */ ) {
        //...

        wc.lpfnWndProc = WndProc;
        RegisterClass(&wc);
        HWND theWnd = CreateWindow(wc, /* ... */ );
        SetWindowLong(theWnd,MY_OFFSET, this);
    }

    virtual BOOL MsgProc(HWND hDlg, UINT message, WPARAM wParam,
        LPARAM lParam) = 0;

};

I.e. when we register the class, we always use the same (static) member
function.  We also store the class' address in a long with the window.
Then when the callback function gets called, it retrieves the address of
the class that created the window, and invokes the virtual callback
function in that class.

 PJ> I have a function which is called several times. It allocated memory
 PJ> for some text (using new). Obviously I don't want to leave all this
 PJ> allocated, and because it needs to remain allocated after the function
 PJ> call, I'd like each call to free the ram allocated by the last call.
 PJ> Obviously if it's safe to free a pointer which should still be NULL,
 PJ> that'd be great - simplify the code loads. Question is... is it safe?

It should be safe as long as you store its address in a static variable.
This may also be a reasonable situation in which to turn the function
into a class, which can make maintaining state between calls easier.
    Later,
    Jerry.

... The Universe is a figment of its own imagination.

--- PPoint 1.90
 * Origin: Point Pointedly Pointless (1:128/77.3)