srajeshnkl
srajeshnkl

Reputation: 855

C++ Dynamic object construction

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,

Upvotes: 0

Views: 350

Answers (2)

Wei Song
Wei Song

Reputation: 605

Well, before anything useful comments, you forget to declare them as public and forget to declare the inherit relation in your class definitions.

To your question, why not directly declare the ParseHeader function as a public member method of the base class Msg. I cannot see any problems if you doing so.

There may be some dependence problem. You need to put the method as a declaration in Msg and define the body in a cpp file. Something like:

// in Msg.h
// all other lines are omitted
public:
  Msg* ParseHeader(string data);


// in Msg.cpp
#include "Msg.h"
#include "InitiateMsg.h"
#include "ReadOperationMsg.h"
#include "WriteOperationMsg.h"

Msg* Msg::ParseHeader(string data) {
// .....
}

also, if you want to differentiate exactly which class it is when you have a base pointer. If I am doing it, I will declare a emun in the base class to remember it. Something like:

// Msg.h
class Msg{
public:
    enum type_t {
      TBase,
      TInit,
      TRead,
      TWrite
    } type;

Then in each different construction method, define a different type_t to the type variable. As a result you can always tell which class the pointer is pointer to and do dynamic class casting without miss trials.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206518

You are in need of Abstract Factory Design pattern. It is custom made for a scenario like yours.
The inline link explains in much more in detail with an simple example than I could here.

Upvotes: 4

Related Questions