Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now