c*****e 发帖数: 737 | 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 | M****g 发帖数: 162 | | f*******l 发帖数: 66 | 3 1 2 编译正确,结果B 3
3 5 编译正确
4,6 编译正确,运行错误
【在 M****g 的大作中提到】 : 1,2,3,5编译正确,4,6运行错误?
| h********w 发帖数: 221 | 4 能解释下为啥 4,6 编译正确,运行错误呢?
当然欢迎你都做下说明啦
【在 f*******l 的大作中提到】 : 1 2 编译正确,结果B 3 : 3 5 编译正确 : 4,6 编译正确,运行错误
| q****x 发帖数: 7404 | 5 http://www.cplusplus.com/doc/tutorial/typecasting/
【在 c*****e 的大作中提到】 : 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
| c*****e 发帖数: 737 | 6 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";}
30 virtual void fd() {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
还有加两行
D* p3 = dynamic_cast(new B(3, 5)); //5
p3->fd(); //6 | M****g 发帖数: 162 | | f*******l 发帖数: 66 | 8 1 2 编译正确,结果B 3
3 5 编译正确
4,6 编译正确,运行错误
【在 M****g 的大作中提到】 : 1,2,3,5编译正确,4,6运行错误?
| h********w 发帖数: 221 | 9 能解释下为啥 4,6 编译正确,运行错误呢?
当然欢迎你都做下说明啦
【在 f*******l 的大作中提到】 : 1 2 编译正确,结果B 3 : 3 5 编译正确 : 4,6 编译正确,运行错误
| q****x 发帖数: 7404 | 10 http://www.cplusplus.com/doc/tutorial/typecasting/
【在 c*****e 的大作中提到】 : 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
| h*****f 发帖数: 248 | 11 You will get a warning for the "incorrect" order of initialization for line
#14 as well.
Running 1, 2, 3 will have no warning or error.
#4 and 5 will have a runtime error (not compilation error as cplusplus.com
mentions).
#2 will have a compilation error (for all compilers?) if virtual D::foo()
isn't defined since the compiler doesn't know whether D::foo() should be
inherited from B or C.
#6 will have a runtime error because p3's vtable pointer is pointing to B's
vtable, and B has no virtual function called fd() (in other words, adding B:
:fd() won't help either, but adding virtual B::fd() does). | h*****f 发帖数: 248 | 12 hmm..actually for #6, if I add void fd() to A (non virtual), how come it
doesn't work?
I thought p3->fd() would call A::fd() since B inherets A...any idea? | h*****f 发帖数: 248 | 13 nevermind.
D::fd() is declared virtual, and so the compiler is only looking at the
vtable but not the "inherent table". | s******n 发帖数: 3946 | 14 no compiling error,
2 gets the right result
4 and 6 is getting NULL pointer segfault from dynamic_cast of 3 and 5
tell interviewer to fuck himself, 3,4,5,6 make sense in some real
programming cases.
who writes program like 1&2? |
|