I have a base class,
class Msg
{
public:
ParseMsg()
{
ParseMsgData();
ParseTrailer();
}
virtual void ParseMsgData() = 0;
ParseTrailer();
};
and derived classes,
class InitiateMsg : public Msg
{
public:
void ParseMsgData() { ... }
};
class ReadOperationMsg public Msg
{
public:
void ParseMsgData() { ... }
};
class WriteOperationMsg public Msg
{
public:
void ParseMsgData() { ... }
};
and the scenario is below,
void UsageFunction(string data)
{
Msg* msg = ParseHeader(data);
ParseMsg
}
Msg* ParseHeader(string data)
{
Msg *msg = NULL;
....
switch()
{
case 1:
msg = new InitiateMsg();
break;
case 2:
msg = new ReadOperationMsg{();
break;
case 3:
msg = new WriteOperationMsg{();
break;
....
}
return msg;
}
based on the data ParseHeader method will decide which object has to be created, So I have implemented ParseHeader function outside the class where I am using. How can I make the ParseHeader function inside the Msg class and then use it?
In C# the same is achieved by defining ParseHeader method as static with in class and use it from outside,