I intend to adjust (or gain info about) the functioning of a program without changing anything to the actual code. I originally hoped this could be done by using the C++ operator overloading functionality, but first tests are not satisfying.
Concrete
I have a simple function in sample.c file:
void dunno() {
int x = 1;
int y = 4;
int z = x + y;
}
To alter the functionality I create a header file which (in my head) would looked like the following:
...
int operator+(int x1, int x2) // this syntax is obviously not allowed by C++?
{
// Change and gain knowledge here!
return something here;
}
#include "sample.c"
...
With other words: I try to alter the real meaning of for example two ints that are being added. And most important when the unmodified function tries to add two ints my operator overloaded function should be called instead of the standard C int+int…
Sorry guys, it’s hard to decently explain what I intend to say.
You could potentially sort of make this work by writing a value-wrapper class for each primitive you want to intercept (or making it a template on the storage type), and then using macros, like so:
but this is going to get unimaginably hairy and you really shouldn’t do it.
The key thing to understand is that you’d be transforming the code so much by doing this, it’s hard to be sure how much the behaviour is changed. Also, it will just break the any headers included by
sample.cwhich declare out-of-line functions or variables.A better approach is probably just to step through the code in a debugger, if you really want to see exactly what it does (or, if suitable, template
dunnoon its data type so you can pass a debugging value type without macro hackery).