下面哪个选项在这种情况下是正确的?
Which of the following will be true in this case?
我在测试中被问到这个问题,我不知怎么弄糊涂了:
Q. If the following is a part of a completely fine C++ program:
p = new bicycle("Hello world", 2,4);
then, which of the following is true?
1. p is a pointer to a concrete class bicycle.
2. p is a pointer to a concrete class bicycle or some base class (abstract) of bicycle.
3. p is a pointer to a concrete class bicycle or some derived class of bicycle.
Options:
A) 1 only
B) 2 only
C) 3 only
D) 1 and 2 both
你能告诉我正确的解决方案是什么吗?我想应该是:bicycle * p = new ....
正确答案是B
bicycle *p = new bicycle();
对于p
,这可以是一个有效的声明。
此外,可以将指向派生类的指针存储在基类的指针中。例如,有一个类Cycle
, bicycle
继承Cycle
,然后::
Cycle *p = new bicycle();
也是一个有效的语句
这意味着p
可以是指向具体类bicycle 或的某个基类bicycle(这里是cycle)的指针。因此,答案是B
而且,答案不能是D,因为p
不能是一个具体指针和也是一个基类指针。感谢@bogdan。
虽然很多事情仍然不清楚从这个问题。考虑到p
也可以是void *
(再次感谢@bogdan:P),这个问题显然错过了。
相关文章:
- 我是c ++的新手,你能解释一下在这种情况下的指针吗
- 在这种情况下,java对象是否可以调用本机函数
- 在这种情况下显式调用时,std::cout 如何更改析构函数的行为?
- 在这种情况下,我真的复制了字节还是复制了字符?
- 为什么在这种情况下,bool 类型的输出等于 0?
- 在这种情况下,如何传递成员函数而不是函数?
- 为什么在这种情况下递增阵列名称有效?
- 在这种情况下我应该使用哪种设计模式
- 为什么在这种情况下我需要 .template
- 在这种情况下,使用 string_view 是否会导致不必要的字符串复制?
- 我是否访问了已释放的内存,或者在这种情况下DrMemory报告不正确?
- 在这种情况下,"typename..."意味着什么?
- 为什么在这种情况下 x = 44?
- 在这种情况下是私有的吗?试图使操作员<<过载
- 在这种情况下,如何防止C++输出/控制台窗体关闭
- 为什么 lambda nullptr 取消引用在这种情况下有效?
- 为什么在这种情况下,前向声明不起作用?
- 为什么在这种情况下不调用我的虚拟函数实现?
- 在这种情况下,为什么模板即时深度超过限制?
- 下面哪个选项在这种情况下是正确的?