How to use dynamic_cast?

Hi!

I am trying to write a agile programm by writing a factory. The factory is using a messaghandler, the messagehandler is evaluating a string and depending of the result the factory is creating a command object that will be executed the code is this
[code/]
   Database db;
   CommandFactory sf;
   .............
   ...........  
   while (true) {
        Connection* conn = server.waitForActivity();
         if (conn != 0) {
               MessageHandler mh(conn);
               try {
                  Command c = sf.make(mh);
                  c.execute(mh, db);
               }
               catch (ConnectionClosedException&) {
                  server.deregisterConnection(conn);
                  delete conn;
                 cout << "Client closed connection" << endl;
              }
          }
          else {
            server.registerConnection(new Connection);
            cout << "New client connects" << endl;
        }
    }
[/code]
the factory is immplemented like this
[code/]
  Command make(const MessageHandler& mh) {
       .......
       ......
       if(mh.getMess() == 12)
          return AddCommand();
  }
[/code]
the AddCommand class
[code/]
 class AddCommand : public Command {
        public:
            void execute(const MessageHandler& mh, const Database& db) ;
};
class Command {
        public:
            virtual void execute(const MessageHandler& mh, const Database& db) = 0;
};
[/code]


the problem is that i get a error
commandfactory.h:9: error: invalid return type for member function `client_server::Commandclient_server::CommandFactory::make(const client_server::MessageHandler&)'

should i use dynamic_cast and how should i use it?

mikrodidaktAsked:
Who is Participating?
 
sunnycoderCommented:
> Command make(const MessageHandler& mh)
Command has abstract virtual functions ... you cannot return objects of this type since objects of such type cannot be constructed. Try returning pointer to object instead.

Cheers!
sunnycoder
0
 
DineshJolaniaCommented:
return AddCommand();
will also give the same problem. Here  also you need to return pointer to Addcommand.
0
 
Prashant SabnekarAVPCommented:
Dear,
the syntax for dynamic cast is,
dynamic_cast<terget class*>(&available obj);
dynamic cast is performed for classes having parent & child relationship.
Eg:
class Base{ ...};
class Derived:public Base{ ...};

base* b;
dynamic_cast<Derived*>(b);
//here b points to the b subobject in Derived object.

Overall i do not understand your question. try to make it brief and more specific.
All the best
Prashant Sabnekar
0
 
sunnycoderCommented:
Hi mikrodidakt,

Thanks for the accept, but you chose B garde which means answer was not precise. Please refer to the grading guidelines
http://www.experts-exchange.com/help.jsp#hi73

Was something not clear or missing from the answer?

Cheers!
sunnycoder
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.