Jump to content
  • Advertisement
Sign in to follow this  
King of Men

Help me with casting

This topic is 3813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have classes SuperClass and SubClass. Both have a virtual method 'eval'. I have some code that does this:
SuperClass* aSuper = gottenFromSomewhere();
if (aSuper->isCastable_to_SubClass) {
  SubClass* aSub = (SubClass*) aSuper;
  return aSub->eval();
}
else return aSuper->eval()
Now, I have an object which I know is returning true for the castability test, because I have printouts in my if block saying "Yep, we're casting the bugger." But nonetheless, it is the SuperClass::eval method that is being called, again checked by printouts. So, is there something about casting I don't understand? Surely having been cast to a SubClass*, aSub->eval() should end up in SubClass::eval. And yet it doesn't. I'm confused.

Share this post


Link to post
Share on other sites
Advertisement
SuperClass* aSuper = gottenFromSomewhere();
since your eval method is virtual it will execute the eval method of whatever the type of the pointed to object is.

for example if gottenFromSomewhere() returns a pointer to a SuperClass object it will still execute SuperClass's eval even if the pointer is of SubClass type.

maybe what you want to do is this:
SuperClass* aSuper = new SubClass(); (no cast required)
aSuper->eval();
which will execute SubClass::eval()

if you are trying to do SubClass's eval on a SuperClass object, that is not a good idea.

you might want to do some reading on virtual functions:
http://en.wikipedia.org/wiki/Virtual_function

[Edited by - fishmd on January 9, 2008 9:38:04 PM]

Share this post


Link to post
Share on other sites
Is there any particular reason for why you're not using dynamic_cast?

Share this post


Link to post
Share on other sites
There is indeed: I never ran into this problem before, and therefore didn't know about dynamic casting. Thank you. :) Learn something new every day.

Share this post


Link to post
Share on other sites
Stop! Listen to fishmd. Why aren't you using virtual functions? This is their very purpose in life.

Every time you cast, god kills a kitten.

Share this post


Link to post
Share on other sites
Quote:
Original post by King of Men
There is indeed: I never ran into this problem before, and therefore didn't know about dynamic casting. Thank you. :) Learn something new every day.


dynamic casting is used to cast safely throughout a class hierarchy.
since you said eval is virtual the function that will be executed is determined by the type of the pointed to object at run time. In your case, i think your SuperClass pointer actually points to a SuperClass object.
So since eval is virtual, it wont matter what type the pointer is.

SuperClass *t = new SuperClass();
SubClass *s = dynamic_cast<SubClass*>(t);
(i dont know if this is undetermined behavior by the standard)
s->eval() will execute the SuperClass eval function since s points to a SuperClass object.

Share this post


Link to post
Share on other sites
Quote:
Original post by fishmd
SuperClass *t = new SuperClass();
SubClass *s = dynamic_cast<SubClass*>(t);
(i dont know if this is undetermined behavior by the standard)
s->eval() will execute the SuperClass eval function since s points to a SuperClass object.


It's pretty darn clearly spelled out in the standard that s will contain a null pointer. It's derferencing s that will produce undefined behaviour.

Share this post


Link to post
Share on other sites
Quote:
Original post by the_edd
Stop! Listen to fishmd. Why aren't you using virtual functions? This is their very purpose in life.

Every time you cast, god kills a kitten.


Well, actually, I am using virtual functions. That's why I was confused by the problem in the first place.

That said, I've come to the conclusion that the need to cast in that particular place, plus all the other little kludges that have been creeping in lately, show that I was doing some bad design to start with. Fortunately I'm not very far into the project; I'll go back and redesign properly from scratch.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!