Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-02-10
1
Medium Priority
?
288 Views
Last Modified: 2015-02-10
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
0
Comment
Question by:jxbma
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 40601369
'shared_ptr()' has cast members that take care of that, see

http://www.boost.org/doc/libs/1_46_0/libs/smart_ptr/shared_ptr.htm#static_pointer_cast 
http://www.boost.org/doc/libs/1_46_0/libs/smart_ptr/shared_ptr.htm#const_pointer_cast
http://www.boost.org/doc/libs/1_46_0/libs/smart_ptr/shared_ptr.htm#dynamic_pointer_cast

MSDN has code examples with their docs, e.g. https://msdn.microsoft.com/en-us/library/bb982967.aspx ("dynamic_pointer_cast"):

// std_tr1__memory__dynamic_pointer_cast.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
struct base 
    { 
    virtual ~base() 
        { 
        } 
 
    int val; 
    }; 
 
struct derived 
    : public base 
    { 
    }; 
 
int main() 
    { 
    std::shared_ptr<base> sp0(new derived); 
    std::shared_ptr<derived> sp1 = 
        std::dynamic_pointer_cast<derived>(sp0); 
 
    sp0->val = 3; 
    std::cout << "sp1->val == " << sp1->val << std::endl; 
 
    return (0); 
    } 

Open in new window


The others are linked on that page.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

879 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question