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

dynamic_cast vs static_cast-polymorphism 99% sure its solved

Post by SpaceAnimation » April 16th, 2014, 8:49 pm

What's the difference?

What are the most common crashes?

How can we use them with polymorphism?
Last edited by SpaceAnimation on April 21st, 2014, 11:36 pm, edited 1 time in total.
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 16th, 2014, 11:03 pm

from what i know, they cast from derived class to base class (and vice-versa)
difference between both is that dynamic_cast does some run-time type check (rtti) while static_cast doesn't
like for ex:

Code: Select all

class A {
    ...
};

class B : public A {
    ...
};

A* a_var = new B;
B* b_var = new B;

A* a2_var = new A;
B* b2_var = new A;
based on the above the following would happen

Code: Select all

dynamic_cast< B* >( a_var );    // rtti check succeeds since a_var points to a B object
dynamic_cast< A* >( b_var );    // succeeds since A is base of B
static_cast< B* >( a_var );     // same as 1st line above, tho no rtti check
static_cast< A* >( b_var );     // same as 2nd line above, no rtti check ofcourse :p

dynamic_cast< B* >( a2_var );    // rtti fails since a2_var doesn't point to a B object
dynamic_cast< A* >( b2_var );    // succeeds since it points to an A object
static_cast< B* >( a2_var );     // succeeds since no rtti check, dangerous :P
static_cast< A* >( b2_var );     // succeeds, same as 2nd line above
(can't quite remember all the uses it may have, and maybe some of the above would be wrong xD)
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 17th, 2014, 9:51 am

Yea seems like it might be useful when casting during runtime. I have read that dynamic_cast is useful when using polymorphic classes, though I am having trouble using it. I can compile fine but when I try use a function from a derived class that I have dynamic casted, I get a crash (possible that it is returning a nullptr, though I run a condition that if NOT nullptr execute some code - use derived function.

Im trying to use it with physX API and obviously I can change the API to suit my needs as I only want to implement my own makeshift code to use their already made polymorphic classes (I get scared having to use code I know very little about, though after looking at their definition it looks like it should work - I have probably greatly misunderstood how dynamic_cast's are used).

What I want
Store polymorphic shape's into a std::vector<baseClass>
Pass a shape into a function
Then run a derived function on that shape if shape is a correctly derived shape
- some shapes can move and others do not
- therefore only the shapes that move have an applyForce(vec3(x,yz))
Given that I have a container of different shapes:
- i loop through the std::vector<baseClass> and pass each shape to the function that will run
applyForce(vec3(x,yz))
- i can only pass the base shape in and then cast the shape to the appropriate derived class
for the shapes that are able to move
- or just cast the shape to the appropriate derived class when passing it
- therefore I want to use dynamic_cast to run the ritti check during "runtime" and only
the shapes that are from derived class that are moveable with have the function
applyForce(vec3(x,yz)) applied to them given whether they are NOT a nullptr

Brief look at the PhysX classes to be used

Code: Select all

class BaseShape
{
   baseShape();
   virtual void createShape(parameters....) = 0;
};
class MoveableShape : public baseShape
{
    moveableShape();
    virtual void createShape(parameters....);
    applyForce(vec3 force);
};
class staticeShape : public baseShape
{
    staticeShape();
    virtual void createShape(parameters....);
    //given this shape will never move it doesn't need and applyForce(vec3 force);
};

void applyForceIfCanMove(baseShape* shapeCanMove)
{
    if(shapeCanMove != nullptr)
    {
         if(spaceBarPressed)
         {
               shapeCanMove->applyForce(vec3(0,100,0));
         }
    }
}

int main()
{
   std::vector<baseShape*> allShapes;
   baseShape* newShape;
   newShape = new moveableShape;
   newShape.createShape(parameters....);
   allShapes.push_back(newShape);
   newShape = new staticeShape
   newShape.createShape(parameters....);
   allShapes.push_back(newShape);

   for(auto shape : allShapes)
   {
        applyForceIfCanMove(dynamic_cast<moveableShape*>(shape));
   }
   return 0;
}
[/size]

But is crashes at runtime when dynamic_cast is used. Though, it works when static_cast is used????
WHY? :x
Last edited by SpaceAnimation on April 17th, 2014, 11:22 am, edited 1 time in total.
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 17th, 2014, 11:20 am

the doc for dynamic_cast says that type-id (what's in between the <>) can only be a pointer to class, a reference to class or pointer to void, so for your code to work you'd have to use

Code: Select all

applyForceIfCanMove( dynamic_cast< moveableShape* >( shape ) );
(so yea, had that mistake in my post above xD)
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 17th, 2014, 11:24 am

LuisR14 wrote:the doc for dynamic_cast says that type-id (what's in between the <>) can only be a pointer to class, a reference to class or pointer to void, so for your code to work you'd have to use

Code: Select all

applyForceIfCanMove( dynamic_cast< moveableShape* >( shape );
(so yea, had that mistake in my post above xD)
... yea sorry (cheers for that) that was just a typo ... rofl , edited the code in post above... so not the reason why it has a crash.
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 17th, 2014, 2:33 pm

the rest of your code has all those typos? :P (not quite sure why it's not working, if code looks fine, albeit the typos :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
SpaceAnimation
Posts: 245
Joined: July 15th, 2013, 3:31 am

Re: dynamic_cast vs static_cast and polymorphism

Post by SpaceAnimation » April 17th, 2014, 3:29 pm

@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).

I get a crash during runtime when using dynamic_cast but not when using static_cast
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 17th, 2014, 4:36 pm

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 @.@
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 17th, 2014, 5:18 pm

All goods. U confused me with your riddles. Rofl
I'm not sure what's going on either. Someone out there might. Someone...
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 17th, 2014, 6:04 pm

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.
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

Post Reply