Boost logo

Boost Users :

From: Scott Amort (jsamort_at_[hidden])
Date: 2006-08-13 18:03:34


Hello,

I am in the process of converting an application that uses raw pointers
to boost::shared_ptr. For example:

ErrorWriter *writer_;

now becomes:

typedef boost::shared_ptr<ErrorWriter> ErrorWriterPtr;

ErrorWriterPtr writer_;

The problem is that, with raw pointers I was able to use 0-value
pointers in the constructor, like so:

Parser(ErrorWriter *writer)
  : writer_(writer)
  {
  }

Parser testParser(0);

So it was possible to initialize the class with an empty writer pointer,
and then later in some other Parser code do this:

if (writer_ == 0) { /* create default error writer */ }

But now, with shared_ptr, I cannot assign or evaluate to 0. I
understand that the default ErrorWriterPtr constructor and
ErrorWriterPtr.reset() will provide me with an empty pointer that I can
check like so:

if (writer_.get() == 0) { /* create default error writer */ }

But how do I rewrite the Parser constructor to fit this new idiom? If I
have:

Parser(ErrorWriterPtr writer)
  : writer_(writer)
  {
  }

What is the equivalent to:

Parser testParser(0);

Thank you for any assistance!

Best,
Scott


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net