I''m writing a simple chat application, and I''m implementing a CMessage class. CMessage will be a base class (not an ADT, but still acting like one), with children like CChatMessage, CBootMessage, or CDrawMessage. Now, in order to keep it as OO as possible, I thought of having a "smart" constructor for CMessage which would allow me to say: new CMessage(netConnection), and the object would actually be the right kind of CMessage. I don''t think I''m explaining well - maybe this will:
//Grabs a message from the socket queue.
CMessage * msg = new CMessage(netConnection);
msg->Process();
CMessage::CMessage(CConnection * con)
{
BYTE messageType;
con->Read(sizeof(BYTE),&messageType);
switch(messageType)
{
case CHAT_MESSAGE:
delete this;
//Would have to implement another constructor for CChatMessage to avoid this one being called recursively.
this = new CChatMessage(netConnection);
break;
case BOOT_MESSAGE:
//....
break;
}
}
My question is this: Is this a gross misuse of the language, or acceptable OO programming practice? I''m guessing that it''ll be the former, but I''m just checking...
BTW, I have tried this technique, and it works. I just dunno if it''s safe etc.
---------------
#define TRUE 0
#define FALSE 1
//MUAHAHAHAHAAAA!