C++ std::shared_ptr usage and information

后端 未结 3 557
梦如初夏
梦如初夏 2020-12-15 06:35

I am trying to use std::shared_ptr in my code. I have seen there have been other questions on the subject, but I am still getting a compiler error. Have I got t

相关标签:
3条回答
  • 2020-12-15 07:25

    If you don't have shared_ptr in std you can use it from boost.

    #include <boost/shared_ptr.hpp>
    
    boost::shared_ptr<A> ptr_A( new A() );
    
    0 讨论(0)
  • 2020-12-15 07:27

    std::tr1::shared_ptr is part of the TR1 additions to the C++ STL.
    With GCC, it is available either through #include <tr1/memory> (GCC 4.1) or #include <memory> (GCC 4.3)

    0 讨论(0)
  • 2020-12-15 07:33

    You were also asking for references or literature...

    I found 3 articles that may help:

    • An article on Smart Pointers, which is an overview good for a general understanding.
    • An actual reference for std::shared_ptr.
    • A great tutorial discussing every method of TR1 shared_ptr along with sample code.

    Also a comment on your code example:

    std::shared_ptr<A*> ptr_A = shared_ptr( new A() ); 
    

    The template argument should be A instead of A* :

    std::shared_ptr<A> ptr_A = shared_ptr( new A() ); 
    
    0 讨论(0)
提交回复
热议问题