コンテンツの書き込み/読み取りまたは変更を検出できるように、すべての演算子がオーバーロードされたラッパー クラスを作成したいと考えています。例えば:
probe<int> x;
x = 5; // write
if(x) { // read
x += 7; // modify
}
誰かがすでにそれをしましたか?そうでない場合、何も見逃さないようにするには、どの演算子をオーバーロードする必要がありますか?
コンテンツの書き込み/読み取りまたは変更を検出できるように、すべての演算子がオーバーロードされたラッパー クラスを作成したいと考えています。例えば:
probe<int> x;
x = 5; // write
if(x) { // read
x += 7; // modify
}
誰かがすでにそれをしましたか?そうでない場合、何も見逃さないようにするには、どの演算子をオーバーロードする必要がありますか?
これを共通のアイデアとして使用してください。&= |= [] のような演算子はたくさんありますが、これらはおそらくあなたのケースではプリンシパルではありません。
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;
}
};
できないと思います。operator?: オーバーロードできません。また、T::T(int)
定義されている場合T foo = 4
は合法ですが、そうでT foo = probe<int>(4)
はありません。ユーザー定義の変換は最大で 1 つです。
さらに、probe は POD ではないため、プログラムの動作が変わる可能性があります。