C++ review questions

Assume that there are already four classes A, B, C, and D defined. What is the order in which the destructors of A, B, C, and D are called in the program? ( )

C c;

void main()

{

 A*pa=new A();

 B b;

 static D d;

 delete pa;

}


Analysis: First, pa is released manually, so the destructor of A will be called first, and then the local objects will be released in the reverse order of the definition. There is only b here, so release b, then release the static local object d, and then release the global object c.

Which of the following statements is wrong ( ). (Alibaba 2015 written test questions)

A.引用必须初始化,指针不必
B.引用初始化以后不能被改变,指针可以改变所指的对象
C.不存在指向空值的引用,但是存在指向空值的指针
D.一个引用可以看作是某个变量的一个“别名”
E.引用传值,指针传地址
F.函数参数可以声明为引用或指针类型

A. The reference must be initialized. It must be clear which variable or object is referenced when defining the reference. Otherwise, there will be a syntax error. When the pointer is not initialized, the value will point to a random point.

B. Once a reference is initialized and specified when it is defined, it cannot be modified, and the pointer can be changed to point to

C. References must be exposed, no null references can occur, and pointers can be assigned to null.

D. A simple and crude understanding of reference can be understood as an "alias" of the referenced variable or object.

E. On the surface, the reference seems to be passing by value, but its essence is also passing the address. It is just that the compiler does this work, so it is wrong.

F. In order to improve efficiency in function calls, references or pointers are often used to pass variables or objects as function parameters.

おすすめ

転載: blog.csdn.net/AkieMo/article/details/131891714