Shared pointers and const correctness

Viewed 566

Which is the correct way to extend the const correctness of a class to its pointed members? In the example code, is the constant version of the get method going to create an std::shared_ptr whose reference counter is the same as the internal member m_b, or is it starting again counting from 0?

class A
{
    std::shared_ptr< B > m_b;

public:

    std::shared_ptr< const B > get_b( ) const
    {
        return m_b;
    }

    std::shared_ptr< B > get_b( )
    {
        return m_b;
    }
}
2 Answers
Related