# include <iostream> # include <stdio.h> class A { public: A() :i(10) { } virtual void f() { std::cout << "A::f() i = " << i << std::endl; } int i; }; class B : public A { public: B() :j(20) { } virtual void f() { std::cout << "B::f() j = " << j<< std::endl; } int j; }; int main() { A *a = new A(); std::cout << "A *a = new A(); " << std::endl; std::cout << "a->i = " << a->i << std::endl; A b; std::cout << "A b; " << std::endl; std::cout << "A c; " << std::endl; A c; std::cout << "b.i = " << b.i << std::endl << std::endl;; int* p = (int*)&b; int* p2 = (int*)&a; int* p3 = (int*)&c; int* p4 = (int*)*p; std::cout << "int* p4 = (int*)*p; the *p4 is " << *p4 << std::endl; std::cout << "int* p4 = (int*)*p; the p4 is " << p4 << std::endl; std::cout << "int* p3 = (int*)&c; the *p3 is " << *p3 << std::endl; std::cout << "int* p = (int*)&b; the *p is " << *p << std::endl; std::cout << "*p2 = " << *p2 << std::endl; std::cout << "a is a point : A* a = new A(); the sizeof(a) is " << sizeof(a) << std::endl; std::cout << "b is a object : A b; the sizeof(b) is " << sizeof(b) << std::endl<<std::endl; p++; p2++; std::cout << "*(p++) = " << *p << std::endl; std::cout << "p++ = " << p << std::endl; std::cout << "*(p2++) = " << *p2 << std::endl; std::cout << "p2++ = " << p2 << std::endl; /***********************************************************/ A d; B e; A* pa = &e; pa->f(); char cc = getchar(); return 0; }
代码运行结果:
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。