Here is the code,
I have two questions here. Actually B's size is 8 bytes and D's size is 24 bytes. First question is why diff is 1 instead of 8? The second question why b[2].getValue() gives access violation exception since b points to a chuck of size 24*10. Thanks.Code:#include <iostream> using namespace std; class B { public: virtual int getValue(){return 1;} private: int _x; }; class D : public B { virtual int getValue() {return 2;} private: double _y; int _z; int _w; }; int _tmain(int argc, _TCHAR* argv[]) { B* b = new D[10]; int diff = &b[2] - &b[1]; int x = b[2].getValue(); return 0; }


Reply With Quote
Bookmarks