Solved

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

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

830 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