Skip to content

Instantly share code, notes, and snippets.

@kkdai
Created February 14, 2014 01:37
Show Gist options
  • Select an option

  • Save kkdai/8994287 to your computer and use it in GitHub Desktop.

Select an option

Save kkdai/8994287 to your computer and use it in GitHub Desktop.
class AA
{
public:
AA()
{
printf("1");
}
virtual void F1()
{
printf("2");
}
void F2()
{
printf("3");
}
~AA()
{
printf("4");
}
}
class BB : public AA //BB繼承AA
{
public:
BB()
{
printf("5");
}
virtual void F1()
{
printf("6");
}
void F2()
{
printf("7");
}
~BB()
{
printf("8");
}
}
void main()
{
AA v1; // 1
v1.F1(); // 2
v1.F2(); // 3
BB v2; // 4
v2.F1(); // 5
v2.F2(); // 6
AA *v3 = new BB(); // 7
v3->F1(); // 8
v3->F2(); // 9
delete v3; // 10
BB *v4; // 11
v4->F1(); // 12
v4->F2(); // 13
BB *v5 = new BB(); // 14
v5->F1(); // 15
v5->F2(); // 16
delete v5; // 17
}
@kkdai
Copy link
Copy Markdown
Author

kkdai commented Feb 14, 2014

然後以下是我的答案

1: 1 11: (沒東西)
2: 2 12: (應該會有 segment fault)
3: 3 13: 7
4: 1 5 14: 1 5
5: 6 15: 6
6: 7 16: 7
7: 1 5 17: 8 4
8: 6 main結束前: 8 4 4
9: 3
10: 4

然後你的疑問可能有

  • 4,7,14: 別鬧了同學, 先回去仔細讀完繼承...
  • 11: 這也別鬧了, 這裡只有一個指標宣告啊 orz
  • 10,17: 差別在 v3 和 v5 的型態
  • 12,13: 差別在有沒有用到 vptr
  • main 結束前: 請看 1 和 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment