您好,登錄后才能下訂單哦!
# 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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。