生物信息学分析 你想要什么结果就给你什么结果# Biology - 生物学
c*e
1 楼
1 #include
2
3 class A
4 {
5 public:
6 A(int v_) : v(v_) {};
7 virtual void foo() {std::cout << "A:" << v << "\n";}
8 int v;
9 };
10
11 class B : public A
12 {
13 public:
14 B(int w_, int z_):w(w_), A(z_){};
15 virtual void foo() {std::cout << "B:" << w << "\n";}
16 int w;
17 };
18
19 class C : public A
20 {
21 public:
22 C(int k): A(k){};
23 virtual void foo() {std::cout << "C:" << v << "\n";}
24 };
25
26 class D : public B, C
27 {
28 public:
29 D(int v_, int w_, int x_) : C(v_), B(w_, v_), x(x_) {};
30 virtual void foo() {std::cout << "D" << w << "," << x << "\n";}
31 int x;
32 };
33
34 int main()
35 {
36 D* p = static_cast(new B(3, 5)); //1
37 p->foo(); //2
38 D* p2 = dynamic_cast(new B(3, 5)); //3
39 p2->foo(); //4
40 }
有4个选项
编译警告
编译错误
编译正确,运行错误
编译正确,运行结果是?
分别对1,2,3,4做出选择。
好像还问了,如果D没有virtual foo,那么调用D对象指针的时候call哪个?
最后写出D的virtual table
2
3 class A
4 {
5 public:
6 A(int v_) : v(v_) {};
7 virtual void foo() {std::cout << "A:" << v << "\n";}
8 int v;
9 };
10
11 class B : public A
12 {
13 public:
14 B(int w_, int z_):w(w_), A(z_){};
15 virtual void foo() {std::cout << "B:" << w << "\n";}
16 int w;
17 };
18
19 class C : public A
20 {
21 public:
22 C(int k): A(k){};
23 virtual void foo() {std::cout << "C:" << v << "\n";}
24 };
25
26 class D : public B, C
27 {
28 public:
29 D(int v_, int w_, int x_) : C(v_), B(w_, v_), x(x_) {};
30 virtual void foo() {std::cout << "D" << w << "," << x << "\n";}
31 int x;
32 };
33
34 int main()
35 {
36 D* p = static_cast
37 p->foo(); //2
38 D* p2 = dynamic_cast
39 p2->foo(); //4
40 }
有4个选项
编译警告
编译错误
编译正确,运行错误
编译正确,运行结果是?
分别对1,2,3,4做出选择。
好像还问了,如果D没有virtual foo,那么调用D对象指针的时候call哪个?
最后写出D的virtual table