b******e 发帖数: 34 | 1 C is a derived class from B, which of the following is true:
B* C:: fn(C* c)
{
if (c !=0)
{
c->clear();
return c;
}
else{
return NULL;
}
}
1. This will compile and run successfully
2. This will compile, but may cause an addressing exception at run time.
3. This will not compile.
4. More info is needed to determine whether this will compile and run. |
s*********y 发帖数: 689 | 2 题目风格像是IKM的C++考题,我也不确定答案是哪个。
返回NULL的指针可以么?待会用g++试着编译一下看看。
【在 b******e 的大作中提到】 : C is a derived class from B, which of the following is true: : B* C:: fn(C* c) : { : if (c !=0) : { : c->clear(); : return c; : } : else{ : return NULL;
|
y*******g 发帖数: 6599 | 3 1
【在 b******e 的大作中提到】 : C is a derived class from B, which of the following is true: : B* C:: fn(C* c) : { : if (c !=0) : { : c->clear(); : return c; : } : else{ : return NULL;
|
b******e 发帖数: 34 | |
b********u 发帖数: 63 | 5 if you assume member function clear() exists for C, then answer is 1,
because you will always call C::clear(). |
t**g 发帖数: 1164 | 6 但是允许把null类型的指针转换为B*么?
在C++里不允许吧
记得C++编译器还是蛮严谨的
【在 b********u 的大作中提到】 : if you assume member function clear() exists for C, then answer is 1, : because you will always call C::clear().
|
J*****n 发帖数: 4859 | |
b********u 发帖数: 63 | 8 of course you can. just like B* pB = NULL;
【在 t**g 的大作中提到】 : 但是允许把null类型的指针转换为B*么? : 在C++里不允许吧 : 记得C++编译器还是蛮严谨的
|
S*********r 发帖数: 42 | 9 4.
If B has virtual function, it can be compiled. If B do not, it will not be
compiled.
This function, B* C:: fn(C* c), uses dynamic casting. |
p**o 发帖数: 61 | 10 I thought when use dynamic casting, you can cast from derived class C to
base class B without require B to have virtual function.
If you go from base to derived, virtual function is required.
【在 S*********r 的大作中提到】 : 4. : If B has virtual function, it can be compiled. If B do not, it will not be : compiled. : This function, B* C:: fn(C* c), uses dynamic casting.
|