4

As mentioned here you can use reference (d-reference) instead of pointer (d-pointer) in case of PIMPL idiom.

I'm trying to understand if there are any serious issues with this implementation and what are the pros and cons.

Pros:

  • Shorter syntax because of usage of "." instead of "->".
  • ...

Cons:

  • What if the new ObjectPivate() fails and new doesn't throw (e.g.: new(std::nothrow) or custom new) and returns nullptr instead? You need to implement additional stuff to check if the referance is valid. In case of pointer you just use:

if (m_Private)
  m_Private->Foo();

  • In rare case of multiple constructors for the Object with complex initialisation logic the solution could be not applicable. [© JamesKanze]
  • It fills more natural to use pointer for memory management. [© JamesKanze]
  • Some additional implementation details needs to be considered (use of swap()) to ensure the exception-safety (e.g. implementation of assignment operator) [© Matt Yang]
  • ...

Here the sample code for illustration:

// Header file

class ObjectPrivate;

class Object
{
public:
 Object();
 virtual ~Object();

 virtual void Foo();

 private:
   ObjectPrivate&  m_Private;
};

// Cpp file

class ObjectPrivate
{
public:
  void Boo() { std::cout << "boo" << std::endl; }
};

Object::Object() :
m_Private(* new ObjectPrivate())
{
}

Object::~Object()
{
  delete &m_Private;
}

void Object::Foo()
{
  m_Private.Boo();
}
Community
  • 1
  • 1
mem64k
  • 728
  • 1
  • 11
  • 25
  • 2
    If `new ObjectPivate()` fails, I think you should find a way to `throw`. No point in having an implementation-less pimpl. – juanchopanza Apr 11 '13 at 09:55
  • @juanchopanza I'm not sure what he means by "fails". `new ObjectPrivate` calls `operator new` (which throws a `bad_alloc` if it fails), and a constructor (which can only report failure with by throwing an exception). – James Kanze Apr 11 '13 at 09:59
  • 1
    @JamesKanze Right. I was addressing OP's **Cons** section, but I guess that scenario would only be possible with `new (std::nothrow)`. – juanchopanza Apr 11 '13 at 10:03

3 Answers3

6

It's really just a matter of style. I tend to not use references in classes to begin with, so using a pointer in the compilation firewall just seems more natural. But there's usually no real advantage one way or the other: the new can only fail by means of an exception.

The one case where you might favor the pointer is when the object has a lot of different constructors, some of which need preliminary calculations before calling the new. In this case, you can initialize the pointer with NULL, and then call a common initialization routine. I think such cases are rare, however. (I've encountered it once, that I can recall.)

EDIT:

Just another style consideration: a lot of people don't like something like delete &something;, which is needed if you use references rather than pointers. Again, it just seems more natural (to me, at least), that objects managing memory use pointers.

James Kanze
  • 142,482
  • 15
  • 169
  • 310
1

It's not convenient to write exception-safe code I think.

The first version of Object::operator=(Object const&) might be:

Object& operator=(Object const& other)
{
    ObjectPrivate *p = &m_Private;
    m_Private = other.m_Private;        // Dangerous sometimes
    delete *p;
}

It's dangerous if ObjectPrivate::operator=(ObjectPrivate const&) throws exception. Then what about using a temporary variable? Aha, no way. operator=() has to be invoked if you want change m_Private.

So, void ObjectPrivate::swap(ObjectPrivate&) noexcept can act as our savior.

Object& operator=(Object const& other)
{
    ObjectPrivate *tmp = new ObjectPrivate(other.m_Private);
    m_Private.swap(*tmp);                // Well, no exception.
    delete tmp;
}

Then consider the implementation of void ObjectPrivate::swap(ObjectPrivate&) noexcept. Let's assume that ObjectPrivate might contain a class instance without swap() noexcept or operator=() noexcept. I think it's hard.

Alright then, this assumption is too strict and not correct sometimes. Even so, it's not necessary for ObjectPrivate to provide swap() noexcept in most cases, because it's usually a helper structure to centralize data.

By contrast, pointer can save a lot of brain cells.

Object& operator=(Object const& other)
{
    ObjectPrivate *tmp = new ObjectPrivate(*other.p_Private);
    delete p_Private;
    p_Private = tmp;        // noexcept ensured
}

It's much more elegant if smart pointers are used.

Object& operator=(Object const& other)
{
    p_Private.reset(new ObjectPrivate(*other.p_Private));
}
Matt Yang
  • 571
  • 3
  • 7
1

Some quick and obvious additions:

Pro

  • The reference must not be 0.
  • The reference may not be assigned another instance.
  • Class responsibilities/implementation are simpler due to fewer variables.
  • The compiler could make some optimizations.

Con

  • The reference may not be assigned another instance.
  • The reference will be too restrictive for some cases.
justin
  • 101,751
  • 13
  • 172
  • 222