c++ - Non-const copy constructor -


i'm doing copy on write optimization object (i.e. when calling copy-constructor save pointer object , copy if need change our object, or if object pointing going change).

before changing our object need notify others it, can perform real coping. action decided use observer pattern:

struct subject {     void register_observer(const event& e, observer& obs);     void notify(const event& e) const;  private:     std::map<event, std::vector<observer*> > _observers; }; 

and observers:

struct observer {     virtual void onevent(event e); }; 

then, our object inherits both of them. problem is, in copy-constructor, need call register_observer, non-const method, when const argument:

my_class::my_class(my_class const &other) {     if (other._copy != null) this->_copy = &other;     else {         this->_copy = other._copy;         this->_copy->register_observer(event::changed, *this);     } } 

one possible solution i've discovered use mutable, think doesn't fit there, because object logically changed.

any other ideas?

in case, should use mutable keyword.

your object still remain logically const because, user point of view, nothing has changed.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -