// trivial/silly “policy” struct impl_t { int i; impl_t(int v=0) : i(v) {} int get() {return i; } void set(int v) {i = v; } }; // typedef boost::shared_ptr impl_ptr_t; // quick hack to remove dependency on shared_ptr and template handling for testing using a copyable impl struct impl_ptr_t { impl_t i; impl_ptr_t(impl_t* pimpl) : i(*pimpl) {} impl_t& operator*() {return i;} impl_t* operator->() {return &i;} }; struct proxy_t { impl_ptr_t pimpl; proxy_t(impl_ptr_t pi) : pimpl(pi) {} // proxy_t(impl_t* pi) : pimpl(pi) {} // uncomment as work-around int get() {return pimpl->get(); }; void set(int v) {pimpl->set(v); }; }; // in reality this is of course templated, but I left that detail out as it turns out to be irrelevant... struct policy_user { proxy_t pol; policy_user(const proxy_t& policy) : pol(policy) {} void do_stuff() { pol.set(pol.get()+1); } }; int main() { { // it is of course possible to have an instance of each type in this hierarchy impl_t* imp = new impl_t(5); impl_ptr_t imp_ptr(imp); proxy_t prox(imp_ptr); policy_user user(prox); user.do_stuff(); } { // in reality I might do this instead policy_user user_from_temps( proxy_t( impl_ptr_t( new impl_t(50) ) ) ); user_from_temps.do_stuff(); // generates error indicating decl was parsed incorrectly } { // except that with gcc 3.2 that didn't compile. This does: proxy_t proxy_from_temp( impl_ptr_t( new impl_t(50) ) ); policy_user user_from_temps( proxy_from_temp ); user_from_temps.do_stuff(); } { // optimistically, hope that construction of impl_ptr_t from impl_t* will occur implicitly? policy_user user_from_temps( proxy_t( new impl_t(50) ) ); // compiles user_from_temps.do_stuff(); // no error } }