that's evil. Don't do that.
your answer: it uses dirty pointer arithmetic and casting to acheive it's purposes.
I think this is how it works, but if I'm wrong, somebody please point it out to me:
Code:
#include <iostream>
using namespace std;
static int i;
class x
{
private:
int i;
public:
x() { cout << "In HERE" << endl; }
int geti() { return i; }
};
int main()
{
//this line takes the address of i (the static one) and adds one to it,
//then casts that address to a pointer of type x and uses it's geti
//method. This pretty much creates a pointer to type x in the memory
//location adjacent to i
cout << "Inside main() = " << ((x*)(&i+1))->geti() << endl;
//this assignes the number 17 to what the pointer next to i is pointing
//to
*(&i+1) = 17;
//this is the same as the first line
cout << "Inside main() = " << ((x*)(&i+1))->geti() << endl;
//this actually creates an x and calls it aa
static x aa;
//this uses the geti method in that x
cout << "Inside f() : " << aa.geti() << endl;
//this creates another x, but doesn't use it...
x bb;
}
I changed how it was organized a tad bit, but logically it's the same code. Basically, it just assumes the code is written a certain way, and if you move things around (add another int to the class, throw in another global int under the static one), it will change the outcome of the program.