We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

How to use dynamic_cast?

mikrodidakt
mikrodidakt asked
on
Medium Priority
298 Views
Last Modified: 2010-04-22
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?

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2006
Commented:
> 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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
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
CERTIFIED EXPERT
Top Expert 2006

Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.