Slimelite BBS

C++ Problems

Back to EMULATE

From Doug Nazar · 1996-04-11 20:26 · #73698 in EMULATE

In reply to C++ Problems by Paul Johnston

Hello Paul!

Monday April 08 1996 22:55, Paul Johnston wrote to All:

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

You would have a problem if you needed to "define" each class before the other. However, if you only need to "declare" them, above the definition of class Message, insert

class WriteMsg;

However, be aware that all actions on an object of WriteMsg in class Message require that class WriteMsg be defined. The solution here is to just not define any functions using class WriteMsg in the class definition.


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

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

You need to declare ReadMsg::ReadMsgProc as static.


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?

While technically you can do this, I think this will just cause problems later on. (ie. You call the function & return pointer A, you call it again and return pointer B, then go to use pointer A except it's now invalid!)

I don't know enough about the function to suggest a good alternative but to answer your question, calling 'delete[] 0' is perfectly legal and has no effect.

Doug

--- GoldED/2 2.50+
 * Origin: Realm of the Dragons - `Where thy mind hath no limits' (1:250/516)