How can I dynamically raise and lower using smart pointers?

I have code with a common interface in which I need to swing up and down. I am trying to convert to smart pointers now, but I am encountering some errors. The following code reproduces the problem. I use C ++ 14, so I think this stuff should work Automagically now?

#include <memory> #include <iostream> int main() { std::shared_ptr<int> a(new int); *a = 5; std::shared_ptr<void> b = std::dynamic_pointer_cast<void>(a); std::shared_ptr<int> c = std::dynamic_pointer_cast<int>(b); std::cout << *c; //should be 5 return 0; } 

However, when I try to compile this, I get:

 Error C2680 '_Elem1 *': invalid target type for dynamic_cast Error C2681 'int *': invalid expression type for dynamic_cast 

Guess I did something dumb in the syntax?

+7
c ++ casting dynamic-cast smart-pointers
source share
2 answers

The rules are the same as for blunt pointers, except that you should use std::static_pointer_cast and std::dynamic_pointer_cast instead of static_cast and dynamic_cast . C ++ 17 will provide an analogue for reinterpret_cast , but this is not necessary for transitions from the top or down, as well as for conversions from or to void.

How to make dynamic enhancement ... using smart pointers?

The conversion in the type hierarchy is implicit, so there is no need to create:

 struct A{ virtual ~A(){} }; struct B:A{}; auto b = std::make_shared<B>(); std::shared_ptr<A> a = b; 

How do I dynamically ... reset using smart pointers?

Using std::dynamic_pointer_cast if you are not sure if the source points to the correct type. This is possible only if inheritance is polymorphic (i.e., the Base has at least one virtual function):

 b = std::dynamic_pointer_cast<B>(a); 

Or std::static_pointer_cast if you know that the type is correct:

 b = std::static_pointer_cast<B>(a); 

 std::shared_ptr<void> b = std::dynamic_pointer_cast<void>(a); std::shared_ptr<int> c = std::dynamic_pointer_cast<int>(b); 

void and int have no inheritance relationship, so they cannot be dropped or omitted.

However, all pointers are implicitly converted to a void pointer, so implicit conversion is possible:

  std::shared_ptr<void> b = a; 

And, void pointers can be static in the original type, so you can use std::static_pointer_cast :

 std::shared_ptr<int> c = std::static_pointer_cast<int>(b); 
+5
source share

Since C ++ 17, you are std::reinterpret_pointer_cast

+2
source share

All Articles