Link to home
Start Free TrialLog in
Avatar of mikrodidakt
mikrodidakt

asked on

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?

ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DineshJolania
DineshJolania

return AddCommand();
will also give the same problem. Here  also you need to return pointer to Addcommand.
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
Hi mikrodidakt,

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

Was something not clear or missing from the answer?

Cheers!
sunnycoder