If you cannot even explain your own code , or even its intended purpose then
I don't give a **** what it does or how it does it TBH.
I know what it does, thanks. It calls a non-static member function,
without an instantiated object, with predictable defined results.
Thereby showing that you don't need an object to call a (non-virtual)
member function [You do need one for virtual member functions, for
obvious reasons].
i.e. It does what you have repeatedly claimed to be impossible.
I'll explain it to you, now So pay attention
/*
class A is a POD-struct (no non-public data members, no virtual
functions).
This means that the address of any A object is the address of its
first data member
(an int) which means that I can type-pun between A* and int*
i.e. if sizeof(int) = N, the first N bytes of an "A object", is the
value representation of "num"
*/
class A {
public:
int num;
int another_num;
/*
This getter function returns the value of the first data member,
which for the reasons mentioned above is the same as *(int*)
(address of object)
*/
int f() { return num;}
/*
This setter function sets the first sizeof(N) bytes of the object
to the value representation of x
*/
void setnum(int x) {num = x;}
};
int main()
{
int z = 4;
/* tell the compiler to pretend the object at &z, is an A, not an
int */
A* foo = reinterpret_cast<A*> (&z);
/* Call the getter member function on our fictional A object. This
is ok, because we only access the A->num data member, and we know
thats precisely the storage occupied by z - which conveniently is the
same type. So the first N bytes at that address are the integer value
representation of 4 (the value of z)/ Since the methods aren't
virtual, we don't need to access a vtable, or any type information,
which is good, as there isn't any. We guarantee this compile-time
dispatch because A is POD. That's why I had to have public data
members */
std::cout << f(foo) << std::endl;
/* Call the setter member function on our fictional A object. This
is also ok, for all the same reasons. We set the first N bytes at
address foo = &z to the value representation of 7 */
setnum(foo,7);
/* And now, as if by magic, z is 7. The end. No A object was
created by this code. I lied to the compiler that there was one at
&z. I never allocated any storage, for example for the any data
member A::another_num */
std::cout << z << std::endl;
}
result: the code prints 4, then 7.
See also, the side-by-side code at
http://publib.boulder.ibm.com/infoc.../com.ibm.xlcpp8a.doc/language/ref/cplr035.htm