Search code examples
c++visibilitydestructorpimpl-idiom

PIMPL, POD, visibility of the implementation class, will its destructor get called?


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


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.