Inheritance Question

Started by
3 comments, last by canned 18 years, 11 months ago
I'm building a network protocol and I am using objects to contain the data that I'm sending and receiving over the socket. I have one parent type object, NetworkCommand, and several other objects that implement NetworkCommand, for example NetworkChatCommand. So, for the above two, NetworkChatCommand extends NetworkCommand, adding a string for the chat message. Furthermore, NetworkChatCommand overrides a virtual function from NetworkCommand, parseData(). This function is used to break up the raw data (when the client receives data, it places it in a NetworkCommand and then parseData is called to break it up into length, command type, and the other data). My Problem: When I receive data from the socket, I create a NetworkCommand and pass the data into it. I then can check the byte of the data which tells me what command type it is. Say the message is a Chat message. I want to type cast the NetworkCommand into a NetworkChatCommand and call the parseData function to break it up and extract the chat data. However, since I create the object as a type NetworkCommand, whenever I call the parseData() (even after type casting it), it is calling the function of NetworkCommand and not NetworkChatCommand. Is it possible to make something like this work? I could also have the parseData function in NetworkCommand figure out which command type it is and call the appropriate class's parseData function, but because they are child classes, I cannot call the functions. Also, I assume type casting the (this) pointer would have the same result as above (still calling the parseData function of NetworkCommand instead of the type-casted class). Any ideas on how to get around this? Or perhaps of a better approach in general? Thanks!
Advertisement
If there is only a single byte that defines the type of data, presumably it will be near the beginning of the stream, so I would suggest reading that byte first, and then creating the right class to start with.

You could implement it the way you are trying to, by creating a new NetworkChatCommand, transferring control of the stream to it, and then deleting the original NetworkCommand. However, this seems a little too complicated.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

well it looks to me you just need to use "right" the virtual functions :)
Quote:
My Problem: When I receive data from the socket, I create a NetworkCommand and pass the data into it. I then can check the byte of the data which tells me what command type it is

You should not create NetworkCommand, you need to know the right object you are expecting...
example:
NetworkCommand *p_networkCommand;
p_networkCommand = new NetworkChatCommand(/*constructor init from socket*/);
p_networkCommand->parseData();//will call NetworkCommand parseData method!!!

//if still you can incomporate cration of NetworkChatCommand from your socket i //suggest try smth like this (the NetworkChatCommand and all childs of NetworkComand should have default constructor with parameter NetworkCommand!!!, so the data from it be initialized in every specific NetworkCommand class)
NetworkCommand nk;//you have it from the socket
if(nk.type == type.chatCmnd)
nkc = new NetworkChatCommand(NetworkCommand)
====> then use the example above

please tell me if you get what i mean. i think you simply missing some fundamental c++ and the general OOP concept of polymorphism etc.
If I understand the problem correctly, this is how I would do it:

Network command becomes an abstract base type.

You should have a socket object that encapsulates the functionality of the socket.

The socket object should contain an instance of an object (lets call it a NetworkCommandCreator) that parses the first byte of the stream and then creates an instance of appropriate NetworkCommand.

This NetworkCommand instance then parses the data it is given using its virtual functions.


The main problem is that your NetworkCommand subclasses (NetworkChatCommand etc.) are performing an entirely different action to their base class.

[Edited by - Nitage on May 18, 2005 11:53:26 AM]
Quote:
please tell me if you get what i mean. i think you simply missing some fundamental c++ and the general OOP concept of polymorphism etc.


I get what you mean. I understand exactly why it is happening and it makes perfect senese to me. I was just curios if there was a quick way to get around this. As it seems like there isn't, I've just created a NetworkCommandFactory object that, when taking in the raw data, calculates the type and builds the right type of command for the job.

Thanks for the replies.

This topic is closed to new replies.

Advertisement