   boost::shared_ptr
  Hiya,I'm having a bit of trouble passing pointers, and I was wondering if a Boost shared pointer would solve the problem - I've never really used them before.The issue is that in a constructor, I pass a pointer to another object, which is then stored internally in a member variable. If the stored object is then destroyed, this interal pointer points to an invalid location and the object which contains/uses it will fail. That might not have been too clear, as an example:ILogger *pLogger = CreateRemoteLogger();// pLogger is stored internally, to be used as necessaryIRenderer *pRenderer = CreateRenderer( pLogger );// if the logger is released, the renderer will fail when it tries to use itdelete pLogger;// crash!pRenderer->DoSomethingThatLogsData();Can anyone tell me if using a boost::shared_ptr for the logger would solve this? If not, are there any other methods that will work?Many thanks for any help.James.   
  Yes, it will. If the logger is stored in a boost::shared_ptr instance, it will only be deleted when all shared_ptrs which reference it are destroyed.Other methods that would work are scope based. For example, create a logger instance. Share the logger instance with other objects by reference.For example:int main(){    Logger logger;    Renderer renderer(logger); // Renderer( Logger & )    SomeOtherThing other(logger);    // no need to call delete, everything will be destructed in the correct order}This will work for pointers too, using std::auto_ptr or boost::scoped_ptr:int main(){    std::auto_ptr<ILogger> pLogger = CreateRemoteLogger();    std::auto_ptr<IRenderer>pRenderer = CreateRenderer( *pLogger );}Just be careful with references as member variables. I usually disable the copy constructor and operator= for such objects.   ;  boost::shared_ptr keeps a reference count on the object it is pointing to so that the shared_ptr can be passed around and wherever it last goes out of scope or gets cleaned up will result in the object it is pointing to being destroyed. However this only half solves your problem because it means that the resource may be cleaned up somewhere other than where it is presently.What you could do however is pass a reference to a pointer into the constructor and store a reference to a pointer in the class. This means that when the pointer is freed elsewhere (and presumably set to 0) the pointer in the class will also be invalid.If it doesn't matter where the resource is cleaned up  then use a shared pointer.   ;  Thanks for the quick replies [smile]Quote:Original post by DaveWhat you could do however is pass a reference to a pointer into the constructor and store a reference to a pointer in the class. This means that when the pointer is freed elsewhere (and presumably set to 0) the pointer in the class will also be invalid.What is the syntax for that? Something like the following?class Bar;class Foo{public:    Foo( Bar &*p ) : m_p( p ) {}private:    Bar  &*m_p;};Thanks again.   ;  Found the answer - it needs to be declared the other way round *&Cheers.   ;  It's the other way around:Bar*& p;I would like to add that unless you REALLY know what you are doing i would tend to go for shared_ptr. References to pointers are just a little more icky.   