Link to home
Start Free TrialLog in
Avatar of jxbma
jxbmaFlag for United States of America

asked on

How do I cast smart pointers using dynamic and static casting in Microsoft C++ 11

Hi:

I've got a couple of questions regarding smart pointers and casting in Microsoft C++ 11.
It's been a while since I have done C++. I've been doing .Net/C# for a while now.

Consider the following class definitions and variables:
Class Foo {};
Class FooA : public Foo {};
Class FooB : public Foo {};

shared_ptr<FooA> fooA = make_shared<FooA>();
shared_ptr<FooB> fooB = make_shared<FooB>();
shared_ptr<Foo> someFoo;
shared_ptr<FooA> someFooA;

Open in new window


1) How do I dynamically downcast from fooA to someFoo?

2) How do I do that with a static cast?

3) If we assume that someFoo has been assigned somewhere further upstream,
    how do I dynamically cast someFoo to someFooA?

4) How do I do that statically?

5) If I was using raw pointers, I could do the following
if (dynamic_cast<FooA *>(someOtherFoo) != nullptr)
{
}

Open in new window

What's the best way to do this using smart pointers and casting?

Thanks,
JohnB
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial