1 #include<iostream>
2 #include<windows.h>
3 using namespace std;
4 class T
5 {
6 protected:
7 int t;
8 public:
9 T(int r=0):t(r){}
10 void showNum(){cout<<t<<endl;}
11 };
12 class T1:public T
13 {
14 private:
15 int x;
16 public:
17 T1(int r):x(r),T(r){}
18 void show(){cout<<"x="<<x<<endl;}
19 };
20 class T2:public T
21 {
22 private:
23 int x;
24 public:
25 T2(int r):x(r*r),T(r){}
26 void show(){cout<<"x="<<x<<endl;}
27 };
28 void main()
29 {
30 T* p[5];
31 for(int i=0;i<5;i++)
32 {
33 if(i%2==0)
34 {T1 r(2);p[i]=&r;cout<<&r<<endl;}
35 else
36 {T2 r(3);p[i]=&r;cout<<&r<<endl;}
37 }
38 for(int i=5;i<10;i++)
39 {
40 if(i%2==0)
41 {T1 r(4);p[i]=&r;cout<<&r<<endl;}
42 else
43 {T2 r(5);p[i]=&r;cout<<&r<<endl;}
44 }
45 for(int i=0;i<10;i++)
46 {
47 p[i]->showNum();
48 }
49 system("pause");
50 }


首先为什么输出的地址是这样的,而不是相同的地址(前5个是一组,后5个是一组)
还有后5个地址为什么和前5个地址也不一样?
如果有人能从栈详细解释下就感激不尽不尽了~~
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4547/viewspace-3123593/ ,如需转载,请注明出处,否则将追究法律责任。