c++c++11boostsmart-pointers

Understanding C++ make shared pointer with const arguments


I found somewhere this piece of code:

boost::shared_ptr<const Foo> pFoo = boost::make_shared<const Foo>();

What's the aim of the const keyword here?


Solution

  • Its very simple, it really is just a pointer pointing to a const Foo. The code, currently, which is:

    boost::shared_ptr<const Foo> pFoo = boost::make_shared<const Foo>();
    

    Is the basic equivalent of

    const Foo * pFoo
    

    The meaning of const here is regular as with const pointers The advantage of this is that the pointer is read-only, because of constness