Solved

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

Posted on 2015-02-10
1
226 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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

831 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