谷歌C++编程为何禁止缺省参数
C++的缺省参数尽量不要使用,结果可能出乎我们的意料,下面的程序大家看看输出结果是多少?
1 #include <iostream>
2 using namespace std;
3
4 class A
5 {
6 public:
7 virtual void pt(int ipt, int num = 1)
8 {
9 cout << ipt << endl;
10 cout << num << endl;
11 }
12 };
13
14 class B:public A
15 {
16 public:
17 virtual void pt(int ipt, int num = 2)
18 {
19 cout << ipt << endl;
20 cout << num << endl;
21 }
22 };
23
24 int main()
25 {
26 A *p = new B;
27 p->pt(3);
28 }
2 using namespace std;
3
4 class A
5 {
6 public:
7 virtual void pt(int ipt, int num = 1)
8 {
9 cout << ipt << endl;
10 cout << num << endl;
11 }
12 };
13
14 class B:public A
15 {
16 public:
17 virtual void pt(int ipt, int num = 2)
18 {
19 cout << ipt << endl;
20 cout << num << endl;
21 }
22 };
23
24 int main()
25 {
26 A *p = new B;
27 p->pt(3);
28 }
想要回答3,2的朋友拿起编译器调试一下吧,呵呵