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.” |