Question

Wikipedia claims, in the article on opaque pointers, that

The d-pointer is the only private data member of the class and points to an instance of a struct (which must be a POD since its destructor is not visible)

This is not required in PIMPL and just Wikipedia being typically idiosyncratic isn't it?

I'm taking the lack of d-pointer tag as an answer to my question, but hoping someone might contribute to Wikipedia and/or clarify things. Or just say Wikipedia is awful, last-resort etc :)

The point of my question is, how visible are a nested class's methods when fully declared and defined in the cpp implementation file? Will its destructor get called as expected (the containing class will call delete on it in its destructor)?

_EDIT_ Fixed version, http://en.wikipedia.org/wiki/Opaque_pointer

Was it helpful?

Solution

Yup, it's plain wrong. The destructor of the PIMPL struct must be visible at the point where it's called, which is from the definition of the destructor of the class itself. Put both destructors in the same .cpp file, PIMPL dtor first, and visibility is assured.

There is no reason for the PIMPL dtor to be visible at any other point, since it's not called there.

OTHER TIPS

PIMPL is a disgusting idiom, and I've come to the conclusion that it's more of an anti-pattern.

However, should you insist on using it, you can trivially define the destructor as empty in an implementation file to make full use of any smart pointer you care to use to automatically destruct non-POD implementations.

class Impl;
class Object {
    std::unique_ptr<Impl> impl;
public:
    //...
    ~Object();
};

#include "Impl.h"
// in cpp
Object::~Object() {}

This quick code sample clearly defines Wikipedia as completely wrong- it's more than possible to simply define the destructor in the implementation file to use whatever destructor you want, even smart pointers which require the full definition.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top