判断题¶
流运算符
Inserter << can be used to output all kinds of primitive types, including the pointers.
答案
T
class 与 struct
In C++, struct is actually the same thing as class, except for minor differences in usage.
答案
T
解析
默认访问权限不同,struct 默认成员 public,class 默认 private,其余语法、继承特性完全一致。
构造函数
Constructors are able to be declared as virtual.
答案
F
解析
C++ 语法规定构造函数不能被声明为 virtual(虚函数):
- 虚函数的动态绑定依赖对象的虚函数表,而构造函数执行阶段,对象的虚函数表还没有初始化完成,无法实现多态调用。
- 构造函数是创建对象时直接调用的,调用时机在对象实例化阶段,本身不需要动态绑定的多态特性。
析构函数
Destructors can not be overloaded.
答案
T
解析
析构函数无参数、签名固定,无法重载。
虚函数
Given the following C++ code segment:
class A {
int i;
virtual void f() {}
};
If sizeof(int *) == sizeof(int) == 4, then sizeof(A) == 8.
答案
T
解析
带虚函数的类会隐含一个虚表指针(4字节),成员变量 int i 占 4 字节,总大小为 8 字节。
初始化顺序
Order of initialization in the initial list is the order of their declaration in the list.
答案
F
解析
C++ 中,类成员的初始化顺序由成员在类定义中的声明顺序决定,与构造函数初始化列表内的书写顺序无关。
运算符重载
对单目运算符重载为友元函数时,可以说明一个形参。而重载为成员函数时,不能显式说明形参。
答案
T
解析
在 C++ 中,单目运算符(如 -、!、~、前缀 ++/-- 等)的重载规则是:
- 重载为友元函数时,需要一个显式的形参(即该类的对象或引用),用于接收操作数。
- 重载为成员函数时,不能显式说明形参,因为操作数通过
this指针隐式传递。
静态成员
All the static members of a class, both variables and functions included, need to be defined outside the class.
答案
F
解析
静态成员函数可在类内定义,仅非内联静态成员变量需类外定义。