[c++]shared_ptr类型转换

#include 
#include 
#include 

class base{
 public:
    base(){std::cout << "base" << std::endl;}
    ~base(){std::cout << "~base" << std::endl;}
    void print(){std::cout << "base::print" << std::endl;}
};

class derived:public base{
 public:
    derived(){std::cout << "derived" << std::endl;}
    ~derived(){std::cout << "~derived" << std::endl;}
    void print(){std::cout << "derived::print" << std::endl;}
};

template
std::shared_ptr<
    typename std::enable_if<(!std::is_same::value)
                            &&(std::is_base_of::value),T>::type
>
dyn_cast(std::shared_ptr ptr){
    return std::static_pointer_cast(ptr);
}

int main()
{
    std::shared_ptr b_ptr = std::make_shared();
    b_ptr->print();
    auto d_ptr = dyn_cast(b_ptr);
    d_ptr->print();
    return 0;
}

 

你可能感兴趣的:(Linux,c/c++)