需要一个模式监视任意变量的改变解决思路

需要一个模式监视任意变量的改变
template< typename T >
class WATCH{
private:
char* m_name;
T m_obj;
public:
  void onChanged( void ){}
};






struct nameInfo 
{
char name[30];
}

WATCH< int > s1;
WATCH< struct nameInfo > s2;
WATCH< char*> s3;

我需要一个好的模式, 让使用者在使用时尽量感觉不到WATCH的存在
例如赋值和获取
s1 = 1;
strcpy( s2.name , "name" );
lps = new char[30];
s3 = lps;

一旦他们的值有所改变 WATCH::onChanged就应该收到通知
当然上面的赋值写法这只是一个设想(如果可以我需要这样的友好方式), 有什么好的方法吗?



------解决方案--------------------
typedef void (*ChangeNotify )(void* pParam);
class Watch
{
ChangeNotify fnChangeNotify;
public:
Watch(ChangeNofify fnNotify){fnChangeNofity=fnNotify};
virtual ~Watch(){};
};
class WatchInt:public Watch
{
int nValue;
public:
WatchInt(ChangeNofify fnNotify):Watch(fnNotify){nValue=0;};
virtual ~WatchInt();
void ChangeValue(void nVal){
if(nVal!=nValue){
nValue = nVal; // 值已经改变
fnChangeNotify(NULL); // 此处仅举例说明,参数省略,也不检查通知函数的有效性
}
}
}
void OnChange(void* pParam)
{
// 操作
}
void main()
{
WatchInt wt(OnChange);
wt.ChangeValue(3);//此处OnChange将被调用
}
------解决方案--------------------
楼上跟我的思路差不多 也写了一个
C/C++ code

void Notify(DWORD dwNotify)
{
    //具体的处理
    return;
}

template <class T>
class Watcher
{
private:
    T m_watchPoint;

public:
    Watcher(T t)
    {
        Notify(0);   //赋值操作
        m_watchPoint = t;

    }

    Watcher()
    {
        m_watchPoint = NULL;
    }

    bool operator ==(Watcher<T>&watcher)
    {

        Notify(0);  //判等操作
        return m_watchPoint == watcher.m_watchPoint;
    }

    Watcher<T> & operator *(Watcher<T>&watcher)
    {
        Notify(0);
        return Watcher<T>(m_watchPoint * watcher.m_watchPoint); 
    }

    //等等操作符重载

};

------解决方案--------------------
标准的办法应该木,特定的编译器下有相关的扩展,比如 M$ 的__declspec(property(put=xx,get=xxx)) 啥的可以实现你要的,不过木移植性。。。