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?

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

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

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

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