no viable conversion from 'shared_ptr<class Derived1>' to 'shared_ptr<class Base>' no viable conversion from 'shared_ptr<class Derived1>' to 'shared_ptr<class Base>' Run Code Online (Sandbox Code Playgroud) 我究竟做错了什么?使用static_pointer_castBase类型是否安全?喜欢 -...
dynamic_pointer_cast与dynamic_cast用法类似,当指针是智能指针时候,向下转换,用dynamic_Cast 则编译不...
classBase{};classDerived:public Base {}; ...shared_ptr<Derived>dp1(new Derived);shared_ptr<Base> bp1 = dp1;shared_ptr<Base>bp2(dp1);shared_ptr<Base>bp3(new Derived); casting shared_ptr shared_ptr<Base>base_ptr(new Base);shared_ptr<Derived> derived_ptr;// if static_cast<Derived *...
C++11的方法是:std::dynamic_pointer_cast,如下代码所示: #include <memory>#include <iostream>class A {public:AA(){}virtual ~A() {}};class B : public A {public:B(){}virtual ~B() {}};int main(){// derived class to A classB* d1 = new B();A* b1 = d1;//std::shared_ptr<...
const_pointer_cast reinterpret_pointer_cast返回指定类型中的删除器,若其拥有get_deleter与另一个 shared_ptr 或 nullptr 进行比较operator== operator!= operator< operator<= operator> operator>= operator<=>将存储的指针的值输出到输出流operator<<特化...
error: no matching function for call to ‘std::shared_ptr::shared_ptr(spBase&) spDerived derived = dynamic_cast < spDerived > ( base ); error: cannot dynamic_cast ‘base’ (of type ‘spBase {aka class std::shared_ptr}’) to type ‘spDerived {aka class std::shared_ptr}’ (targe...
std::endl; } }; int main() { std::shared_ptr<Derived> derivedPtr = std::make_shared<Derived>(); // 静态类型转换 std::shared_ptr<Base> basePtr = std::static_pointer_cast<Base>(derivedPtr); basePtr->print(); // 输出: Derived class return 0; ...
<< std::endl; } }; int main() { std::shared_ptr<Derived> derivedPtr = std::make_shared<Derived>(); // 将shared_ptr<Derived>转换为shared_ptr<Base> std::shared_ptr<Base> basePtr = std::static_pointer_cast<Base>(derivedPtr); basePtr->print(); // 输出: Derived class return 0...
(*this,const_cast<std::remove_cv_t<U>*>(ptr));. The assignment to theweak_thisis not atomic and conflicts with any potentially concurrent access to the same object. This ensures that future calls toshared_from_this()would share ownership with thestd::shared_ptrcreated by this raw pointe...
join(); std::cout << "All threads completed, the last one deleted Derived\n"; } 可能的输出: Base::Base() Derived::Derived() Created a shared Derived (as a pointer to Base) p.get() = 0xc99028, p.use_count() = 1 Shared ownership between 3 threads and released ownership from ...