C++ (Qt)class BaseImpl;class DerivedImpl;class Base {public: Base() : impl(new BaseImpl) {} virtual ~Base() { delete Impl; }protected: Base(BaseImpl* i) : impl(i) {}private: BaseImpl* impl;}; class BaseImpl {public: BaseImpl() {} virtual ~BaseImpl();}; class Derived : public Base {public: Derived() : Base(impl = new DerivedImpl) {} virtual ~Derived() {}protected: Derived(DerivedImpl* i) : BaseImpl(i), impl(i) {}private: DerivedImpl* impl;}; class DerivedImpl : public BaseImpl {public: DerivedImpl() {} virtual ~DerivedImpl() {}};
C++ (Qt)class Base {};class Derived : public Base {};Derived *d = new Derived;Base *b = d;
class Derived{private: BaseImpl *impl1; DerivedImpl *impl2;};
#define DECLARE_PRIVATE(Class) \ inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(d_ptr); } \ inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(d_ptr); } \ friend class Class##Private;
C++ (Qt)class _qualified_ptr {public: _qualified_ptr() : _ptr(NULL) {} _qualified_ptr(void* ptr) : _ptr(ptr) {} inline operator void*() { return _ptr; } inline operator const void*() const { return _ptr; }private: void* _ptr;};
C++ (Qt)#define D_CAST(Class) \ Class##Private* d = static_cast<Class##Private*>((void*)__impptr)#define F_CAST(Class) \ Class* f = static_cast<Class*>((void*)__ifptr)#define D_CAST_CONST(Class) \ const Class##Private* d = static_cast<const Class##Private*>((const void*)__impptr)#define F_CAST_CONST() \ const Class* f = static_cast<const Class*>((const void*)__ifptr)
C++ (Qt) #include <iostream> #define D_CAST(Class) \ Class##Private* d = static_cast<Class##Private*>((void*)__impptr) #define F_CAST(Class) \ Class* f = static_cast<Class*>((void*)__ifptr) #define D_CAST_CONST(Class) \ const Class##Private* d = static_cast<const Class##Private*>((const void*)__impptr) #define F_CAST_CONST() \ const Class* f = static_cast<const Class*>((const void*)__ifptr) class _qualified_ptr {public: _qualified_ptr() : _ptr(NULL) {} _qualified_ptr(void* ptr) : _ptr(ptr) {} inline operator void*() { return _ptr; } inline operator const void*() const { return _ptr; } private: void* _ptr;}; class Base {public: Base(); virtual ~Base(); void foo(); virtual void boom(); protected: Base(void*); _qualified_ptr __impptr;}; class BasePrivate {public: BasePrivate(Base* base); virtual ~BasePrivate(); int x; void bar(); protected: void* __ifptr;}; BasePrivate::BasePrivate(Base* base) : __ifptr(base) { std::cout << "BasePrivate::BasePrivate(Base*)" << std::endl; }BasePrivate::~BasePrivate() { std::cout << "BasePrivate::~BasePrivate()" << std::endl; } void BasePrivate::bar(){ F_CAST(Base); std::cout << "BasePrivate::bar()" << std::endl; (void)f;} Base::Base() : __impptr(new BasePrivate(this)) { std::cout << "Base::Base()" << std::endl; };Base::Base(void* ptr) : __impptr(ptr) { std::cout << "Base::Base(void*)" << std::endl; }Base::~Base() { D_CAST(Base); delete d; std::cout << "Base::~Base()" << std::endl; } void Base::foo(){ D_CAST(Base); std::cout << "Base::foo()" << std::endl; d->bar();} void Base::boom() { D_CAST(Base); std::cout << "Base::boom(), BasePrivate::x = " << d->x << std::endl;} class Derived : public Base {public: Derived(); virtual ~Derived(); void baz(); void baz() const; virtual void boom();protected: Derived(void*);}; class DerivedPrivate : public BasePrivate {public: DerivedPrivate(Derived* derived); ~DerivedPrivate();}; DerivedPrivate::DerivedPrivate(Derived* derived) : BasePrivate(derived) { std::cout << "DerivedPrivate::DerivedPrivate(Derived*)" << std::endl; }DerivedPrivate::~DerivedPrivate() { std::cout << "DerivedPrivate::~DerivedPrivate()" << std::endl; } Derived::Derived() : Base(new DerivedPrivate(this)) { std::cout << "Derived::Derived()" << std::endl; }Derived::~Derived() { std::cout << "Derived::~Derived()" << std::endl; } void Derived::baz() { D_CAST(Derived); std::cout << "Derived::baz()" << std::endl; d->bar();} void Derived::baz() const { D_CAST_CONST(Derived); std::cout << "Derived::baz() const" << std::endl; (void)d; //d->bar(); //error: passing ‘const DerivedPrivate’ as ‘this’ argument of ‘void BasePrivate::bar()’ discards qualifiers [-fpermissive]} void Derived::boom(){ D_CAST(Derived); std::cout << "Derived::boom(), BasePrivate::x = " << d->x << std::endl;} int main(int argc, char** argv){ Base* b = new Base(); b->foo(); delete b; std::cout << "\n ===== \n" << std::endl; b = new Derived(); b->foo(); b->boom(); const Derived* c = dynamic_cast<const Derived*>(b); //c->foo(); // error: passing ‘const Derived’ as ‘this’ argument of ‘void Base::foo()’ discards qualifiers [-fpermissive] delete c; return 0;}