jxbma
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:
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
Thanks,
JohnB
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;
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)
{
}
What's the best way to do this using smart pointers and casting?Thanks,
JohnB
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.