dynamic_cast help?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hijinks@gmail.com

    #1

    dynamic_cast help?

    I am trying to port a python program into c++ to more learn about
    c++... so i am trying to do a MVC design.. so here is my events

    class Event {
    public:
    Event() {}
    Event(string text) { this->name = text; }

    virtual string getName() { return this->name; }

    string name;
    };

    class PlayerSelEvent : public Event {
    public:
    PlayerSelEvent( ) {}
    PlayerSelEvent( string text, string player) : Event(text) {
    this->player = player; }

    virtual string getPlayer() { return this->player; }

    string player;
    };


    now here is how a function gets it

    void AppMasterContro ller::Notify(Ev ent *ev) {
    std::cout << "got an event: " << ev->name << std::endl;

    // checking the type
    if (ev->getName() == "Set Player") {
    PlayerSelEvent *nameEvent = dynamic_cast<Pl ayerSelEvent*>( ev);
    std::cout << "players name is: " << nameEvent->getPlayer() <<
    std::endl;
    }
    }

    no the nameEvent->getPlayer() forces the program to quit due to a run
    time error

    am I doing it right to access the getPlayer() function?? or do I need
    to do something else??

    thanks
    mike

  • Andre Kostur

    #2
    Re: dynamic_cast help?

    hijinks@gmail.c om wrote in news:1131859552 .209495.290960
    @o13g2000cwo.go oglegroups.com:
    [color=blue]
    > I am trying to port a python program into c++ to more learn about
    > c++... so i am trying to do a MVC design.. so here is my events
    >
    > class Event {
    > public:
    > Event() {}
    > Event(string text) { this->name = text; }[/color]

    Should be passing text by const-ref. You're paying for an extra copy,
    and prefer initialization to assignment:

    Event(const string & text) : name(text) {}
    [color=blue]
    >
    > virtual string getName() { return this->name; }
    >
    > string name;
    > };
    >
    > class PlayerSelEvent : public Event {
    > public:
    > PlayerSelEvent( ) {}
    > PlayerSelEvent( string text, string player) : Event(text) {
    > this->player = player; }[/color]

    Same as above... pass by const-ref, and use initialization.
    [color=blue]
    >
    > virtual string getPlayer() { return this->player; }
    >
    > string player;
    > };
    >
    >
    > now here is how a function gets it
    >
    > void AppMasterContro ller::Notify(Ev ent *ev) {[/color]

    Hmm.. why are you accessing the name member directly in the next line,
    but using the accessor function in the if statement?
    [color=blue]
    > std::cout << "got an event: " << ev->name << std::endl;
    >
    > // checking the type
    > if (ev->getName() == "Set Player") {
    > PlayerSelEvent *nameEvent = dynamic_cast<Pl ayerSelEvent*>[/color]
    (ev);[color=blue]
    > std::cout << "players name is: " << nameEvent->getPlayer() <<
    > std::endl;
    > }
    > }[/color]

    Um, why not simply use the result of the dynamic_cast to find out if it's
    the right type?:

    PlayerSelEvent * nameEvent = dynamic_cast<Pl ayerSelEvent *>(ev);

    if (nameEvent != NULL)
    {
    std::cout << "player's name is: " << nameEvent->getPlayer() <<
    std::endl;
    }
    [color=blue]
    >
    > no the nameEvent->getPlayer() forces the program to quit due to a run
    > time error
    >
    > am I doing it right to access the getPlayer() function?? or do I need
    > to do something else??[/color]

    You didn't check the return value of the dynamic_cast to find out if it
    succeeded or not.

    You also haven't shown us how these events were created and passed into
    this function (Notify). It may be possible that you're passing in an
    event with the name "Set Player" that isn't actually a PlayerSelEvent.

    Other items... any reason why the getName and getPlayer methods are
    declared virtual? Are you intending subclasses to be able to change the
    behaviour of those methods? Why no virtual destructor? (You haven't
    shown the allocation/deallocation cycle of Events (and its
    descendants)... I suspect you may be allocating a decendant and may be
    deleteing it through the pointer to Event...). And... too much stuff is
    public, like the name and player member variables.

    Comment

    • John Carson

      #3
      Re: dynamic_cast help?

      <hijinks@gmail. com> wrote in message
      news:1131859552 .209495.290960@ o13g2000cwo.goo glegroups.com[color=blue]
      > I am trying to port a python program into c++ to more learn about
      > c++... so i am trying to do a MVC design.. so here is my events
      >
      > class Event {
      > public:
      > Event() {}
      > Event(string text) { this->name = text; }
      >
      > virtual string getName() { return this->name; }
      >
      > string name;
      > };
      >
      > class PlayerSelEvent : public Event {
      > public:
      > PlayerSelEvent( ) {}
      > PlayerSelEvent( string text, string player) : Event(text) {
      > this->player = player; }
      >
      > virtual string getPlayer() { return this->player; }
      >
      > string player;
      > };
      >
      >
      > now here is how a function gets it
      >
      > void AppMasterContro ller::Notify(Ev ent *ev) {
      > std::cout << "got an event: " << ev->name << std::endl;
      >
      > // checking the type
      > if (ev->getName() == "Set Player") {
      > PlayerSelEvent *nameEvent = dynamic_cast<Pl ayerSelEvent*>( ev);
      > std::cout << "players name is: " << nameEvent->getPlayer() <<
      > std::endl;
      > }
      > }
      >
      > no the nameEvent->getPlayer() forces the program to quit due to a run
      > time error
      >
      > am I doing it right to access the getPlayer() function?? or do I need
      > to do something else??
      >
      > thanks
      > mike[/color]

      I don't know for sure what is causing your runtime error. Selected excerpts
      from your code are rarely adequate to identify a problem. We are not
      psychic. You should supply complete compileable code that exhibits the
      problem. Often this will be a simplified version of your original code.

      I will make a couple of points. First, dynamic casting is rarely needed in a
      well written program. You can generally achieve the same effect in a less
      error-prone way using virtual functions. Second, if you are to use dynamic
      casting, then you might as well use all the power that it offers. You seem
      to be duplicating its operation.

      Judging by your code excerpt, you have a function that takes a base class
      (Event) pointer as an argument. If the pointer actually points to a base
      class object, you want the function to output only the name member from the
      base class. If it points to a derived class (PlayerSelEvent ) object, you
      want the function to output both the name member from the base class and the
      player member from the derived class.

      The simplest way to do this is with a virtual function, Display, defined as:

      virtual void Display()
      {
      std::cout << "got an event: " << getName() << std::endl;
      }

      in Event and

      virtual void Display()
      {
      Event::Display( );
      std::cout << "players name is: " << getPlayer() << std::endl;
      }

      in PlayerSelEvent. Your Notify function is then simply:

      void AppMasterContro ller::Notify(Ev ent *ev)
      {
      ev->Display();
      }

      Incidentally, pev is a much better name for a pointer to an Event than is
      ev.

      If this approach is not possible and you must use dynamic casting, then the
      appropriate function definition is as follows:

      void AppMasterContro ller::Notify(Ev ent *ev)
      {
      std::cout << "got an event: " << ev->name << std::endl;
      PlayerSelEvent *nameEvent = dynamic_cast<Pl ayerSelEvent*>( ev);
      if (nameEvent)
      {
      std::cout << "players name is: " << nameEvent->getPlayer() <<
      std::endl;
      }
      }

      The point here is that the result of dynamic casting will tell you what is
      being pointed to. If it is a PlayerSelEvent object, then the casting works
      and you get the address of the object. If it is an Event object, then the
      casting fails and nameEvent will be zero, indicating that you should not
      attempt to call nameEvent->getPlayer() (doing so will likely cause a runtime
      error). Incidentally, nameEvent would be better called pnameEvent.

      You seem to be attempting to test what is pointed to yourself by testing for
      a name of "Set Player". If a base class object has this name, then your if()
      condition succeeds, nameEvent becomes zero as a result of the dynamic cast,
      and nameEvent->getPlayer() is then an error.


      --
      John Carson

      Comment

      Working...