#include template struct pimpl { class implementation; struct polymorphic_base { protected: explicit polymorphic_base( typename pimpl::implementation * i ) : _impl( i ) {} typename pimpl::implementation * impl() { return _impl.get(); } const typename pimpl::implementation * impl() const { return _impl.get(); } private: boost::shared_ptr _impl; }; template struct derived_from : T_Base { typedef T_Base base; protected: explicit derived_from( typename pimpl::implementation * i ) : base( (typename pimpl::implementation*)i ) {} typename pimpl::implementation * impl() { return (typename pimpl::implementation*)T_Base::impl(); } const typename pimpl::implementation * impl() const { return (const typename pimpl::implementation*)T_Base::impl(); } }; }; class Base : protected pimpl::polymorphic_base { typedef pimpl::polymorphic_base base; public: Base(); void f(); void f() const; protected: explicit Base( pimpl::implementation * i ) : base( i ) {} }; class Derived : public pimpl::derived_from { typedef pimpl::derived_from base; public: Derived(); void g(); void g() const; }; template <> class pimpl::implementation { }; Base::Base() : base( new pimpl::implementation ) {} void Base::f() { pimpl::implementation * i = impl(); //implementation * ii = impl(); // Error: 'implementation' undefined??? } void Base::f() const { const pimpl::implementation * ci = impl(); //pimpl::implementation * i = impl(); // Error: can't convert 'const implementation*' -> 'implementation*': Good. } template <> class pimpl::implementation : pimpl::implementation { }; Derived::Derived() : base( new pimpl::implementation ) {} void Derived::g() { pimpl::implementation * i = impl(); //implementation * ii = impl(); // Error: 'implementation' undefined?? } void Derived::g() const { const pimpl::implementation * ci = impl(); //pimpl::implementation * i = impl(); // Error: can't convert 'const implementation*' -> 'implementation*': Good. } int main() { Base b; Derived d; const Base cb; const Derived cd; b.f(); cb.f(); d.f(); cd.f(); d.g(); cd.g(); return 0; }