class Q_CORE_EXPORT QPoint{public: QPoint(); QPoint(int xpos, int ypos);...#if defined(Q_OS_MAC) int yp; int xp;#else int xp; int yp;#endif};
C++ (Qt)struct Test { private: int mA;};
class A{public: virtual void f(){}private: int x;};class B: public A{public: virtual void f() { std::cout << "is work!" << std::endl; }};int main(int argc, char **argv){ B *b; b = (B*)malloc(sizeof(B)); std::cout << sizeof(*b) << std::endl; //Здесь можно заметить присутствие указателя на vtable new(b) B; A *a = b; a->f();}
C++ (Qt)// do not do that void * place = malloc(sizeof(T));T temp;memmove(place, &temp, sizeof(T));
C++ (Qt)class A{public: virtual void f(){}}; class B: public A{public: virtual void f(){}}; int main(int argc, char **argv){ B *b; b = (B*)malloc(sizeof(B)); new(b) B; A *place = (A*)malloc(sizeof(B)); memmove(place, b, sizeof(B)); if(dynamic_cast<B*>(place)) std::cout << "is work!" << std::endl; B b2; A *place2 = (A*)malloc(sizeof(B)); memmove(place2, &b2, sizeof(B)); if(dynamic_cast<B*>(place2)) std::cout << "is work!" << std::endl;}
C++ (Qt) A( void ) : mA(1) {} virtual void f() { mA += 1; } int mA;}; class B {public: B( void ) : mB(2) {} virtual void f() { mB += 2; } int mB;}; class C : public A, public B {public: C( void ) : mC(3) {} virtual void f() { mA += 3; } int mC;}; template <class T>void PrintVMT( const char * title, T * p ){ if (!p) printf("%s = null", title); else { void ** tst = (void **) p; printf("%s obj = %p, sizeof = %ld, VMT: ", title, p, sizeof(T)); size_t i, num = sizeof(T) / sizeof(void *); for (i = 0; i < num; ++i) printf("[%ld] = %p, ", i, tst[i]); } printf("\n");} int main(int argc, char **argv){ A * a = new A; PrintVMT("(a)", a); B * b = new B; PrintVMT("(b)", b); C * c = new C; PrintVMT("(c)", c); A * testA = dynamic_cast <A *> (c); PrintVMT("(testA)", testA); B * testB = dynamic_cast <B *> (c); PrintVMT("(testB)", testB); return 0;}
(a) obj = 0x1306340, sizeof = 8, VMT: [0] = 0x4028, [1] = 0x1, (b) obj = 0x1306350, sizeof = 8, VMT: [0] = 0x4040, [1] = 0x2, (c) obj = 0x1306360, sizeof = 20, VMT: [0] = 0x4058, [1] = 0x1, [2] = 0x4064, [3] = 0x2, [4] = 0x3, (testA) obj = 0x1306360, sizeof = 8, VMT: [0] = 0x4058, [1] = 0x1, (testB) obj = 0x1306368, sizeof = 8, VMT: [0] = 0x4064, [1] = 0x2,