title: Create smart pointer from raw pointer / reference
body: <p>I am pretty new to C++ and have a problem regarding pointers/references. The following example reflects my problem:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  #include "boost/make_shared.hpp"
  #include "boost/utility.hpp"
  
  class UsedObjectInterface {
   public:
    virtual int data() const = 0;
  };
  
  class UsedObject : public UsedObjectInterface {
   public:
    UsedObject() : data_(42) {
    }
  
    explicit UsedObject(int value) : data_(value) {
    }
  
    int data() const {
      return data_;
    }
  
  private:
    const int data_;
  };
  
  class BaseClient : private boost::noncopyable {
  public:
    virtual const UsedObjectInterface&amp; used_object() const = 0;
  };
  
  class SegmentationFaultClient : public BaseClient {
   public:
    // This can't work, since the object is deleted immediately.
    // IMHO only the following two solutions can work:
    // 1. The member attribute is not a reference (not possible with an abstract class, we lose the advantages of polymorphism).
    // 2. The member attribute is a pointer.
    SegmentationFaultClient() : used_object_(UsedObject()) {
    }
  
    explicit SegmentationFaultClient(const UsedObjectInterface&amp; used_object)
        : used_object_(used_object) {
    }
  
    const UsedObjectInterface&amp; used_object() const {
      return this-&gt;used_object_;
    }
  
   private:
    const UsedObjectInterface&amp; used_object_;
  };
  
  class CorrectClient : public BaseClient {
   public:
    CorrectClient() : used_object_(boost::make_shared&lt;UsedObject&gt;()) {
    }
  
    explicit CorrectClient(const boost::shared_ptr&lt;UsedObjectInterface&gt; used_object)
        : used_object_(used_object) {
    }
  
    // TODO Is it possible to change this to a const&amp;, so at least the interface
    // is the same as in SegmentationFaultClient? Then the above constructor can
    // be deleted.
    explicit CorrectClient(const UsedObjectInterface&amp; used_object)
        : used_object_(&amp;used_object) {
        // TODO How-to convert a raw pointer to a smart pointer?
    }
  
    const UsedObjectInterface&amp; used_object() const {
      return *this-&gt;used_object_;
    }
  
   private:
    const boost::shared_ptr&lt;UsedObjectInterface&gt; used_object_;
  };
  
  int main() {
    SegmentationFaultClient segfault_client;
    const UsedObjectInterface&amp; a = segfault_client.used_object();
    std::cout &lt;&lt; a.data() &lt;&lt; std::endl;
  
    // Correct, but how to make this work with a const&amp; constructor?
    const UsedObject first_object;
    CorrectClient correct_client(first_object);
    const UsedObjectInterface&amp; b = correct_client.used_object();
    std::cout &lt;&lt; b.data() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>As the comments say, the implementaton of the <code>SegmentationFaultClient</code> class is simply wrong, since the default constructor creates an object on the stack, which is "immediately" removed. Therefore I came up with the class <code>CorrectClient</code>, which uses pointers. My goal is to keep the nice API (no public Boost) from <code>SegmentationFaultClient</code> (<code>const&amp;</code> default constructor). The above example does <strong>not</strong> work and terminates with the following error:</p>
  
  <pre><code>invalid conversion from 'const UsedObjectInterface*' to 'boost::shared_ptr&lt;UsedObjectInterface&gt;::element_type* {aka UsedObjectInterface*}' [-fpermissive]
   explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
  </code></pre>
  
  <p>So my question is: Is it possible to convert a raw pointer <code>*</code> to a smart pointer? If so, what is the best way to do that? If you see any other problems with my code, please let me know, too!</p>