inheritance - Prevent call to base assignment operator in C++ -
i have these 2 classes in library:
class base { int _handler; protected: base() = default; base& operator=(int h) { this->_handler = h; return *this; } }; class derived : public base { protected: derived() = default; void initialize() { this->base::operator=(12345); // internal stuff } };
derived
class available inherited user. should this:
class user_class : public derived { void foo() { this->initialize(); } };
but instead, this:
class user_class : public derived { void foo() { this->base::operator=(999); // no, broke it! } };
how can prevent call base
assignment operator?
when change header this
class derived : private base
the compiler blocks call operator= "cannot access inaccessible member declared in class 'base'", code calls initialize works normally, because accessible.
however, should override operator= in derived, , have check whether or not has been initialized. don't make client class handle internal bookkeeping.
Comments
Post a Comment