dynamic_cast vs static_cast-polymorphism 99% sure its solved

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
SpaceAnimation
Posts: 245
Joined: July 15th, 2013, 3:31 am

Re: dynamic_cast vs static_cast and polymorphism

Post by SpaceAnimation » April 17th, 2014, 7:20 pm

LuisR14 wrote:
SpaceAnimation wrote:@Luis its not the typos ... rofl ... my actual code doesn't have typos ... and i cant see any typos apart from the one you pointed out, which now has been fixed. LMAO!

It compiles fine (if it had typos it wouldn't compile).
yea i know lol, just filling up the post space :P
SpaceAnimation wrote:I get a crash during runtime when using dynamic_cast but not when using static_cast
so yea, don't know what's happening in your code to know why it's not working :?: :)

EDIT: without seeing some more code that is lol :lol:

and in my own tests using your method the dynamic cast works, yet it doesn't work for you? o0 @.@
Really... O.o holly shit balls!

The code I gave you was a very simplified version as to what is actually going on. I have only shown a simple version as the code I'm using is written by NVIDIA ( I'm using the PhysX API). Trying to think how I can show the work without uploading the code here - don't think I'm supposed to redistribute the code as a whole). But yea we agree on one thing is that my concept works and I must have a good understanding of polymorphism and dynamic_cast . Hmm so that is promising ...
Spacey :geek:

User avatar
SpaceAnimation
Posts: 245
Joined: July 15th, 2013, 3:31 am

Re: dynamic_cast vs static_cast and polymorphism

Post by SpaceAnimation » April 17th, 2014, 7:27 pm

albinopapa wrote:What is the error message? Have you stepped through the debug to see if dynamic_cast ever returns NULL?

Just did a test of dynamic_cast moving sideways through hierarchy and it did what it was suppose to, returned NULL. Best bet is to step through and debug to see what it is doing, where the error is what it says and hopefully why it isn't doing what is expected. Let us know the outcome.
Yea I did a step though and I get a crash in the code written for dynamic_cast. As from what I know it doesn't have an exception to throw. I'm probably wrong ... as that just seems stupid and it should return a nullptr if bad_cast. Seems weird that is works for static_cast though. Pretty sure the error is an unhandled exception. Will have to boot up the comp and edit this message or ad a snapShot (I'm on the mobile ... LOL XD)
Spacey :geek:

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: dynamic_cast vs static_cast and polymorphism

Post by albinopapa » April 18th, 2014, 12:05 am

Seems like a little detail, but what version of VS are you using, is RTTI enabled?

EDIT:
From what I'm reading on cplusplus.com static_cast will work almost anytime because there is no checks to make sure it is the correct type being converted to.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: dynamic_cast vs static_cast and polymorphism

Post by LuisR14 » April 18th, 2014, 3:39 am

SpaceAnimation wrote:The code I gave you was a very simplified version as to what is actually going on. I have only shown a simple version as the code I'm using is written by NVIDIA ( I'm using the PhysX API). Trying to think how I can show the work without uploading the code here - don't think I'm supposed to redistribute the code as a whole).
well you don't have to upload the PhysX API, just your own files :P
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: dynamic_cast vs static_cast and polymorphism

Post by chili » April 18th, 2014, 1:35 pm

albinopapa wrote:Seems like a little detail, but what version of VS are you using, is RTTI enabled?
I too would check this out under the compiler options.
Chili

User avatar
SpaceAnimation
Posts: 245
Joined: July 15th, 2013, 3:31 am

Re: dynamic_cast vs static_cast and polymorphism

Post by SpaceAnimation » April 19th, 2014, 8:09 am

[img]
dynamic_cast_ERROR.png
(154.57 KiB) Downloaded 57 times
[/img]

I have turned rtti on and I get the above error :?
Spacey :geek:

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: dynamic_cast vs static_cast and polymorphism

Post by LuisR14 » April 19th, 2014, 10:06 am

can't really deduce from just that message .. (so much undefined going on in my head hehe)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
SpaceAnimation
Posts: 245
Joined: July 15th, 2013, 3:31 am

Re: dynamic_cast vs static_cast and polymorphism

Post by SpaceAnimation » April 21st, 2014, 11:33 pm

I think I have the solution after going over my works with LuisR14

We did some research and found out that you can only cast in one direction with "dynamic_cast" - going from derived to base.
but in my case i am wanting to go from base to derived and for this to happen we reluctantly can use "dynamic_cast" - damn no rtti check .. but i suppose that is a good thing as we save on that expensive check.

The good news, turns out "static_cast" is bidirectional" and can cast from base to derived and that is exactly what I want. The only problem with this is that the programer would want to make sure that the class he is casting is in the same hierarchy and is not casting to an ambiguous class within that class hierarchy.

So yea, sometimes life gives you apples and other times it gives you a bad_cast.

Interesting - hopefully we are right for our deduction - if anyone disagrees then please speak up :)

Thanks for helping out luis, you were a great help :) and I really mean that man.
Spacey :geek:

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: dynamic_cast vs static_cast-polymorphism 99% sure its so

Post by albinopapa » April 22nd, 2014, 2:19 pm

Just for clarification I think a base* to derived class can be dynamic_cast to a derived* to derived class, but a base* to base class would give null if dynamic_cast to a derived class.
In other words

Code: Select all

base* a = new derived;
base* b = new base;
derived* c = new derived;

c = dynamic_cast< derived *>( b ); // would work
c = dynamic_cast< derived *>( a ); // should return NULL
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
SpaceAnimation
Posts: 245
Joined: July 15th, 2013, 3:31 am

Re: dynamic_cast vs static_cast-polymorphism 99% sure its so

Post by SpaceAnimation » April 22nd, 2014, 4:39 pm

I have to update my previous post ... as we came across some new findings. With the current class hierarchy the casting can become ambiguous as a parent class can have one or more derived and thus a bad_cast can occur, even we keep casting to the next child class until we reach the desired class to cast.. (ooo man thats hard to write)
Spacey :geek:

Post Reply