FAQ 24.06 When should a user-defined assignment operator mimic the assignment operator that the compiler would generate automatically?

When an ABC defines an assignment operator merely to make it protected:.

When a protected: assignment operator contains the same code that the compiler would have synthesized automatically, its only purpose is to prevent users from assigning to an object of the class. This is common with abstract base classes. Here's an example.

 class Position { }; class Shape { public:   virtual void draw() const throw() = 0;   virtual ~Shape() throw(); protected:   Position pos_;   void operator= (const Shape& s) throw(); }; inline void Shape::operator= (const Shape& s) throw() { pos_ = s.pos_; } Shape::~Shape() throw() { } 

Note that such an assignment operator does not automatically trigger the Law of the Big Three (see FAQ 30.13).



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net