I want to have the following class structure:
#include <tr1/memory>
class Interface;
class Impl;
class Impl
{
public:
Impl( std::tr1::weak_ptr< Interface > interface );
private:
std::tr1::weak_ptr< Interface > interface_;
};
class Interface
{
public:
Interface() { impl_ = new Impl( this ); }
private:
std::tr1::shared_ptr< Impl > impl_;
};
Impl::Impl( std::tr1::weak_ptr< Interface > interface )
: interface_(interface)
{}
The code doesn't work since a weak_ptr can only be constructed from a shared_ptr. I can't construct a shared_ptr of this in the ctor since it would destroy the object when leaving the ctor.
The interface will be held as a shared_ptr by the caller. The Implementation needs to be shared_ptr since its lifetime is longer than the Interface lifetime.
Is there an elegant way to establish this relationship?