feisty2
15th April 2016, 18:14
so I been told (non-static) member function pointers are not exactly "pointers", these abnormal things are hard to use, hard to understand, fatter than actual pointers, and should be avoided if possible.. there're just also hell too many restrictions with them like you're not allowed to reinterpret_cast them to integers and, etc..
but anyway, f**k it, I hate to be bounded by any kind of messy and insanely complicated "rule" like this cuz rules are made to break.
and I thought, I should find a way to cast these bitches to C style function pointers, and things actually worked...
"union" stepped in when "reinterpret_cast" failed
#include <iostream>
#include <cstdlib>
using namespace std;
class _base {
public:
double val = 0.;
};
class a :virtual public _base {
public:
virtual void func() {
val = val + 1.;
cout << val << endl;
}
};
class b :virtual public _base {
public:
virtual void func() {
val = val + 2.;
cout << val << endl;
}
};
class c :public a, public b {
public:
void func() override {
val = val + 3.;
cout << val << endl;
}
};
void main() {
a *obj_a = new a;
b *obj_b = new b;
c *obj_c = new c;
union {
void(a::*fsrc_a)();
void(b::*fsrc_b)();
void(c::*fsrc_c)();
void(*fdst_a)(a *);
void(*fdst_b)(b *);
void(*fdst_c)(c *);
} tmp;
tmp.fsrc_a = &a::func;
void(*func_a)(a *) = tmp.fdst_a;
func_a(obj_a);
func_a(obj_c);
cout << reinterpret_cast<uint64_t>(func_a) << endl;
tmp.fsrc_b = &b::func;
void(*func_b)(b *) = tmp.fdst_b;
func_b(obj_b);
func_b(obj_c);
cout << reinterpret_cast<uint64_t>(func_b) << endl;
tmp.fsrc_c = &c::func;
//tmp.fsrc_c = &a::func; //also works
//tmp.fsrc_c = &b::func; //works, likewise
void(*func_c)(c *) = tmp.fdst_c;
func_c(obj_c);
cout << reinterpret_cast<uint64_t>(func_c) << endl;
delete obj_a;
delete obj_b;
delete obj_c;
system("pause");
}
http://i.imgur.com/imIXl3G.png
everything worked just as expected, like literally..
and now, f**k yeah, I can do whatever the hell I want with them cuz they have been cast to C function pointers
no warning, no error, no unexpected result...
all seemed a little bit too easy, like, I get this feeling like there's gotta be more than this...
Did I miss something?
and I actually wonder what exactly was in the 140697794319020 mem address.. if it was a function, which function exactly was it?
but anyway, f**k it, I hate to be bounded by any kind of messy and insanely complicated "rule" like this cuz rules are made to break.
and I thought, I should find a way to cast these bitches to C style function pointers, and things actually worked...
"union" stepped in when "reinterpret_cast" failed
#include <iostream>
#include <cstdlib>
using namespace std;
class _base {
public:
double val = 0.;
};
class a :virtual public _base {
public:
virtual void func() {
val = val + 1.;
cout << val << endl;
}
};
class b :virtual public _base {
public:
virtual void func() {
val = val + 2.;
cout << val << endl;
}
};
class c :public a, public b {
public:
void func() override {
val = val + 3.;
cout << val << endl;
}
};
void main() {
a *obj_a = new a;
b *obj_b = new b;
c *obj_c = new c;
union {
void(a::*fsrc_a)();
void(b::*fsrc_b)();
void(c::*fsrc_c)();
void(*fdst_a)(a *);
void(*fdst_b)(b *);
void(*fdst_c)(c *);
} tmp;
tmp.fsrc_a = &a::func;
void(*func_a)(a *) = tmp.fdst_a;
func_a(obj_a);
func_a(obj_c);
cout << reinterpret_cast<uint64_t>(func_a) << endl;
tmp.fsrc_b = &b::func;
void(*func_b)(b *) = tmp.fdst_b;
func_b(obj_b);
func_b(obj_c);
cout << reinterpret_cast<uint64_t>(func_b) << endl;
tmp.fsrc_c = &c::func;
//tmp.fsrc_c = &a::func; //also works
//tmp.fsrc_c = &b::func; //works, likewise
void(*func_c)(c *) = tmp.fdst_c;
func_c(obj_c);
cout << reinterpret_cast<uint64_t>(func_c) << endl;
delete obj_a;
delete obj_b;
delete obj_c;
system("pause");
}
http://i.imgur.com/imIXl3G.png
everything worked just as expected, like literally..
and now, f**k yeah, I can do whatever the hell I want with them cuz they have been cast to C function pointers
no warning, no error, no unexpected result...
all seemed a little bit too easy, like, I get this feeling like there's gotta be more than this...
Did I miss something?
and I actually wonder what exactly was in the 140697794319020 mem address.. if it was a function, which function exactly was it?