<$BlogRSDUrl$>

2003/10/21

C++ Datatype Access Counter
Hm, would it be possible to create a template, which overloads the default modification-operators of any datatype/class to be able to track the changes made to a variable?
Of course you couldn't track the custom methods of each datatype, but you could follow the changes through the standard methods.

I'm thinking of something like this (rough draft-code):

template class X {
int counter:
public:
T(){counter=0;}
T& operator=(T& t){counter++; return T::operator=(t);}
T& operator+=(T& t){counter++; return T::operator+=(t);}
T& operator-=(T& t){counter++; return T::operator-=(t);}
T& operator++(){counter++; return T::operator++();}
T& operator++(T&){counter++; return T::operator++(T);}
T& operator--(){counter++; return T::operator--();}
T& operator--(T&){counter++; return T::operator--(T);}

int getAccessCounter(){return counter}
};

Note, that I don't need to overload those operators, which return a copy of the object, because they don't modify the original object.

Then you could replace the variable you want to access-count with this template and at the end of your application you just fetch out the counter.

I still have some doubts that something like this works, but I'm not quite sure, why and how this would fail...

This page is powered by Blogger. Isn't yours? Weblog Commenting by HaloScan.com