Is there a way to cast base class to derived, calling default constructor? I'm receiving network data, writing it to packets, then calling protocol function which I wish to parse data differently.
class BasePacket {
protected:
std::vector<char> data;
public:
BasePacket(){data.reserve(2048);}
BasePacket(char* _data, int len){ data.assign(&data[0], &data[len]); }
};
class ActionPacket : public BasePacket {
public:
char Action;
char Job;
ActionPacket(){
Action = data[0];
Job = data[1];
}
};
I would like to do something like:
void ProcessPacket(BasePacket& packet){
if (packet.data[0] == 1){
auto t = (ActionPacket)packet;
if(t.Job == 1){
//dosmth
}
}
if (packet.data[0] == 2){
auto t = (OtherPacket)packet;
if(t.smth){
//do another smth
}
}
}