How to use dynamic_cast?


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
   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&) {
                  delete conn;
                 cout << "Client closed connection" << endl;
          else {
            server.registerConnection(new Connection);
            cout << "New client connects" << endl;
the factory is immplemented like this
  Command make(const MessageHandler& mh) {
       if(mh.getMess() == 12)
          return AddCommand();
the AddCommand class
 class AddCommand : public Command {
            void execute(const MessageHandler& mh, const Database& db) ;
class Command {
            virtual void execute(const MessageHandler& mh, const Database& db) = 0;

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?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

> 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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
return AddCommand();
will also give the same problem. Here  also you need to return pointer to Addcommand.
Prashant SabnekarAVPCommented:
the syntax for dynamic cast is,
dynamic_cast<terget class*>(&available obj);
dynamic cast is performed for classes having parent & child relationship.
class Base{ ...};
class Derived:public Base{ ...};

base* 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

Was something not clear or missing from the answer?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.