Sign in to follow this  

Read-only shared_ptrs wrapper? (C++) [Solved]

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an object called A which is wrapped up in a boost::shared_ptr. Various internal systems can modify this object, when when all of the shared_ptrs go out of scope the object vanishes. However, some external systems also need access to this object, but they are not permitted to modify it. So, my initial idea was to have the public "Get Object" function return a const boost::shared_ptr, but that would result in the shared_ptr itself being const, not the object it contained. This would lead to errors when the const shared_ptr went out of scope as the reference counting mechanism would fail to work and would probably throw up a compiler error. So, I thought about wrapped a shared_ptr with a class that looks identical, has its own private shared_ptr for all of the reference counting, but only provides const-pointer and const-reference access functions instead of shared_ptr's non-const ones. A quick example of the wrapper:
// Note: this is untested
template <typename T> class shared_const_ptr
{
    public:

        typedef boost::shared_ptr<T> shared_ptr_type;

        shared_const_ptr (T *value = 0) : m_ptr (value)
        {
        }

        shared_const_ptr (const shared_const_ptr<T> &const_ptr) : m_ptr (const_ptr.m_ptr)
        {
        }

        shared_const_ptr (shared_ptr_type ptr) : m_ptr (ptr)
        {
        }

        const *T get () { return m_ptr.get (); }
        void reset (T *value = 0) { m_ptr.reset (value); }

        const *T operator -> () const { get (); }
        const &T operator * () const { return *m_ptr; }

    private:

        shared_ptr_type m_ptr;
};



The end result would be a shared_ptr that can point to any object of type T (and the user can change which object it points to), but the user won't be able to modify the object of T itself. It can also interact with boost::shared_ptr objects too, so there's no chance of objects being deleted prematurely. Is this a good solution, or is there a glaringly obvious answer/problem that I've missed? Thanks :D [Edited by - MENTAL on February 9, 2008 5:09:07 PM]

Share this post


Link to post
Share on other sites
It's not really mix and match. It more or less follows the same rules for const pointers.

boost::shared_ptr<T> a = /* something */;
boost::shared_ptr<const T> b = a; // fine
boost::shared_ptr<T> c = b; // compile error

Though this is why there's a boost::const_pointer_cast() function.

Share this post


Link to post
Share on other sites

This topic is 3595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this