C ++ operator overload monitor

I would like to write a wrapper class with all overloaded operators so that I can detect when we write / read or modify its contents. For instance:

probe<int> x; x = 5; // write if(x) { // read x += 7; // modify } 

Has anyone already done this? If not, which operators should overload to be sure that I'm not missing anything?

+4
source share
2 answers

You can't, I think. operator ?: not overloaded. In addition, if T::T(int) defined, T foo = 4 is legal, and T foo = probe<int>(4) is not. There is no more than one user conversion.

Also, since the probe is not a POD, the behavior of your program may change.

+1
source

Use this as a general idea. There are many operators like & = | = [], which may not be the main ones in your case.

 template < typename T > struct monitor { monitor( const T& data ): data_( data ) { id_ = get_next_monitor_id(); } monitor( const monitor& m ) { id_ = get_next_monitor_id(); m.notify_read(); notify_write(); data_ = m.data_; } operator T() { notify_read(); return data_; } monitor& operator = ( const monitor& m ) { m.notify_read(); notify_write(); data_ = m.data_; return *this; } monitor& operator += ( const monitor& m ) { m.notify_read(); notify_write(); data_ += m.data_; return *this; } /* operator *= operator /= operator ++ (); operator ++ (int); operator -- (); operator -- (int); */ private: int id_; T data_; void notify_read() { std::cout << "object " << id_ << " was read" << std::endl; } void notify_write() { std::cout << "object " << id_ << " was written" << std::endl; } }; 
+2
source

All Articles