I am trying to figure out the cleanest way to implement using a boost::shared_ptr as a member of a class. Let's say I have class A that contains a boost::shared_ptr to a class B member. The B class could be either the base type (B) or a derived type (let's say C or D). This member would be obtained/set continuously throughout the lifetime of class A (using accessors/modifiers). B has to be polymorphic so I can't just store it as B but rather as a reference/pointer to B. I also wanted to avoid having to manage the memory on my own as well. Let's say it all looked like:
class A
{
public:
const B& GetB() const { const B* b = m_pB.get(); return *b; } // Is the correct way to do this?
void SetB(const B& b) { B* b = m_pB.get(); *b = b; } // Is the correct way to do this?
private:
boost::shared_ptr<B> m_pB;
};
I know I'm probably doing something wrong but I wanted to provide an example of what I'm trying to accomplish. Thanks!