Quote:Original post by random_thinker
Thanks for the information, it's helping greatly. But coming back to the issue of class member pointers, copy ctors and assignment operators, I was not aware of such a problem, thank you for educating me on this.
I am surprised that the pointer does not need to be reset to zero (or NULL). So many websites/books recommend this approach.
Also, I use this technique with composition and pimpl type classes. So what you are telling me now, is that there is likely a gaping hole in these classes, and that I either need to declare copy ctors and assignment operators private (use boost::noncopyable) or go with an auto_ptr type technique.
--random
Polymorphic pImpl-pointer (own-a-copy semantics) with virtual clone idiom:
struct Impl { virtual Impl* clone() = 0; virtual void act() = 0;};struct FooImpl { virtual void act() { /* do something */ } virtual FooImpl* clone() { return new FooImpl(this); }};struct BarImpl { virtual void act() { /* do something else */ } virtual BarImpl* clone() { return new BarImpl(this); }};class Wrapper { Impl* impl; public: void act() { impl->act(); } Wrapper(Impl* impl): impl(impl) {} ~Wrapper() { delete impl; } Wrapper(const Wrapper& other): impl(other.impl->clone()) {} Wrapper& operator=(const Wrapper& rhs) { Wrapper other(rhs); std::swap(impl, other.impl); return *this; }};