id like to write a function that checks if an object's variables have been changed. the only way i can think of is to create a duplicate object and compare each variable. im really lazy, and this seems like alot of effort. can anybody help me think outside the box? thanks!

im really lazy, and this seems like alot of effort.

Well that's too bad, because here you have to make an effort in order for us to make an effort to help you.

Your idea is good, just don't let the idea overwhelm you, do it in parts. Does this function have to work with different data types too?

Raw guess here, but maybe you could make a duplicate object and use parallel arrays , one of which is constant and the other one variable, and then fill them with the initial values of the objects' attributes. Then when they change, make the variable arrays elements change likewise and compare :/

You could use some of the object orientation stuff , i.e. if all the variables in an object are private, then you can add a flag that they have been changed, when they are accessed via the setters/public methods.

Alternative, use a checksum. I would keep two numbers (a) the total memory and (b) MD5 sum. That would give >99.99% accuracy.

If you need 100% certain then create a copy (using the copy constructor and assingment operator) and compare. Depending on the class, you are going to have to be careful about what constitutes a difference e.g. comparing to doubles without a tolerance is normally going to not give equallity even if they are actually equal due to rounding errors. You can get hurt by the late on things like compiler sequences e.g (a+b)+c can differ from a+(b+c) .

So in short, a little context is needed or a reasonable amount of thought about what you are trying to achieve. An old programming saying I have heard is Programming is maintaining two goals, the exact logic of the silicon and the illusion the user requires .
[sorry if I have quoted that incorrectly but the sense is valid]

This article has been dead for over six months. Start a new discussion instead.