由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ 一题
相关主题
C++ online Test 又一题C++ Q36: derivation specification (B8_9)
问一个c++ 函数指针的问题你们看过programming pearls (2nd edition English) or 正在看的同学们
请问为什么这个程序会出现RunTime Errorone c++ question
为什么我这段简单的程序segment faultC的argc问题
一题c++ 程序一问
懒得写了,想练手的就写写贴在这里吧bloomberg assessment的机经,c语言的(20道题)
virtual table存在memory的哪块啊?这题哪错了?
Interview questions, BloombergC++: Q75 copy constructor 问什么用 const reference?
相关话题的讨论汇总
话题: derived话题: base话题: class话题: accessible话题: virtual
进入JobHunting版参与讨论
1 (共1页)
h*****g
发帖数: 312
1
class Base{
public:
Base();
virtual ~Base();
};
class Derived: protected Base{
public:
virtual ~Derived();
};
int _tmain(int argc, _TCHAR* argv[])
{
Base *pd = new Derived;
getchar();
return 0;
}
Referring to the sample code above, which one of the following statements is
true?
A.
A constructor needs to be added to Derived class.
B.
The pointer returned by new cannot be type cast from Derived* to Base*.
C.
A pointer to a Base class cannot point to an instance of a Derived class.
D.
Derived class cannot have a virtual destructor.
E.
The code compiles with no errors.
答案是B。但查了半天没看懂为啥?看懂的能不能给详细的解释一下?
f****4
发帖数: 1359
2
class Derived: public Base{}; // is-a relationship
class Derived: protected Base{}; // has-a relationship
class Derived: private Base{}; // has-a relationship
F**********r
发帖数: 237
3
i'm not sure about this either. note if you have Derived *pd = new Derived;
it will compile in gcc. 谁给指点一下这是哪条c++ rule啊。。。
l******l
发帖数: 66
4
You cannot access the base class from Derived because of protected
inherience.
h*****g
发帖数: 312
5
难道是要call baseclass的ctr to cast from Derived* to Base*?

【在 l******l 的大作中提到】
: You cannot access the base class from Derived because of protected
: inherience.

F**********r
发帖数: 237
6
在书上找到了解释,这么说的:
“Like an inherited member function, the conversion from derived to base may
or may not be accessible. Whether the conversion is accessible depends on
the access label specified on the derived class' derivation. To determin
whether the conversion to base is accessible, consider whether a public
member of the base class would be accessible. If so, the conversion is
accessible; otherwise, it's not.”
1 (共1页)
进入JobHunting版参与讨论
相关主题
C++: Q75 copy constructor 问什么用 const reference?一题
这个看着很白痴的问题有更好的解法吗?懒得写了,想练手的就写写贴在这里吧
帮看看这段codevirtual table存在memory的哪块啊?
问一道kth smallest element的题目Interview questions, Bloomberg
C++ online Test 又一题C++ Q36: derivation specification (B8_9)
问一个c++ 函数指针的问题你们看过programming pearls (2nd edition English) or 正在看的同学们
请问为什么这个程序会出现RunTime Errorone c++ question
为什么我这段简单的程序segment faultC的argc问题
相关话题的讨论汇总
话题: derived话题: base话题: class话题: accessible话题: virtual