Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > dynamic_cast help?

Reply
Thread Tools

dynamic_cast help?

 
 
hijinks@gmail.com
Guest
Posts: n/a
 
      11-13-2005
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 AppMasterController::Notify(Event *ev) {
std::cout << "got an event: " << ev->name << std::endl;

// checking the type
if (ev->getName() == "Set Player") {
PlayerSelEvent *nameEvent = dynamic_cast<PlayerSelEvent*>(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

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      11-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:1131859552.209495.290960
@o13g2000cwo.googlegroups.com:

> 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; }


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) {}

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


Same as above... pass by const-ref, and use initialization.

>
> virtual string getPlayer() { return this->player; }
>
> string player;
> };
>
>
> now here is how a function gets it
>
> void AppMasterController::Notify(Event *ev) {


Hmm.. why are you accessing the name member directly in the next line,
but using the accessor function in the if statement?

> std::cout << "got an event: " << ev->name << std::endl;
>
> // checking the type
> if (ev->getName() == "Set Player") {
> PlayerSelEvent *nameEvent = dynamic_cast<PlayerSelEvent*>

(ev);
> std::cout << "players name is: " << nameEvent->getPlayer() <<
> std::endl;
> }
> }


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

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

if (nameEvent != NULL)
{
std::cout << "player's 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??


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.
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      11-13-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com
> 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 AppMasterController::Notify(Event *ev) {
> std::cout << "got an event: " << ev->name << std::endl;
>
> // checking the type
> if (ev->getName() == "Set Player") {
> PlayerSelEvent *nameEvent = dynamic_cast<PlayerSelEvent*>(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


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:isplay();
std::cout << "players name is: " << getPlayer() << std::endl;
}

in PlayerSelEvent. Your Notify function is then simply:

void AppMasterController::Notify(Event *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 AppMasterController::Notify(Event *ev)
{
std::cout << "got an event: " << ev->name << std::endl;
PlayerSelEvent *nameEvent = dynamic_cast<PlayerSelEvent*>(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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
typeid and dynamic_cast, gcc 3.3 Andreas Sch. C++ 18 01-29-2004 10:24 PM
typeid() faster than dynamic_cast<> Jamie Burns C++ 11 01-29-2004 08:54 PM
how static_cast and dynamic_cast implemented? Yuming Ma C++ 1 12-17-2003 12:58 AM
dynamic_cast and references Dan Noland C++ 0 07-29-2003 09:43 PM
dynamic_cast<> alg C++ 3 07-14-2003 09:08 AM



Advertisments