由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ online Test 2题
相关主题
C++做题,麻烦师傅们再看看。Test your C++ knowledge...
An interesting C++ compile errorhow to create an instance of base class in its derived class definition
C++ questionC++问题几个
template 类的继承问题急问:compile and build dependency
A C++ compiler related interview questionHow to understand the answer.
c++ dynamic cast问一个 copy constructor 的问题 (C++)
C++ template problemquestion about c++ constructor
如何 initialize array member?C++ 问题
相关话题的讨论汇总
话题: derived话题: base话题: class话题: cannot话题: ensure
进入Programming版参与讨论
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。但查了半天没看懂为啥?看懂的能不能给详细的解释一下?难道是要call
baseclass的ctr to cast from Derived* to Base*?
class A
{
public:
void f{};
protected:
A(){};
A(const A&){}
}
Referring to the sample code above, why are the default and copy
constructors declared as protected?
A.
To ensure that instances of A can only be created by subclasses of A
B.
To ensure that instances of A cannot be created via new by a more derived
class
C.
To ensure that instances of A cannot be copied
D.
To ensure that A cannot be instantiated on the stack
E.
To ensure that A cannot be used as a base class
答案给的是A。 那如何由子类去创造 A 的object 呢?A * p= new subclass 可行吗?
能call A的ctr吗?
N***m
发帖数: 4460
2
is it because of protected inheritance?

【在 h*****g 的大作中提到】
: class Base{
: public:
: Base();
: virtual ~Base();
: };
: class Derived: protected Base{
: public:
: virtual ~Derived();
: };
: int _tmain(int argc, _TCHAR* argv[])

1 (共1页)
进入Programming版参与讨论
相关主题
C++ 问题A C++ compiler related interview question
c++ initialize structc++ dynamic cast
C++小插曲C++ template problem
一个 C++ STL base type 的问题如何 initialize array member?
C++做题,麻烦师傅们再看看。Test your C++ knowledge...
An interesting C++ compile errorhow to create an instance of base class in its derived class definition
C++ questionC++问题几个
template 类的继承问题急问:compile and build dependency
相关话题的讨论汇总
话题: derived话题: base话题: class话题: cannot话题: ensure