Q027#
Answer:
click to see answer
BQ029#
Answer:
click to see answer
121Explain:
Even though A::foo()
is a virtual function, it is not considered virtual during construction and destruction.
Before constructing B, A should be constructed, and during the construction of A, B is not yet constructed, so it should not be used, the foo()
called in A::A()
naturally calls A::foo()
.
When the object b
is destroyed, the same problem arises because B's destructor is called first, followed by A's destructor, and after the object b
is destroyed, B::foo()
should not be used.
Q114#
Answer:
click to see answer
AAABBAABExplain:
C *const p1
const modifies the p1
pointer, so when foo
is called on the member variable p1
of object s
, the non-const version of foo
will be used. C const *p2
const modifies the *p
(C object), so when foo
is called on the member variable p2
of object s
, the const version of foo
will be used.
r
is a constant reference to object s
, and const
changes the behavior of member v
because std::vector
overloads operator[]
and the version called by a constant object returns a constant reference, so the returned value is also a constant reference, so calling foo
will use the const version.
Q360#
Answer:
click to see answer
0110001Explain:
const int *
is a pointer to a constant integer, which means a constant pointer, although the content pointed to by the pointer is constant, the pointer itself is not constant, so the result is false
.
int *const
is a constant pointer, the pointer itself is constant, so the result is true
.
const int[1]
is an array containing constant integers, the elements of the array are constants, so the result is true
.
const int **
is a pointer to a pointer to a constant pointer ( (const int *) *ptr
), the pointer itself is not constant, so the result is false
.
int *const *
is a pointer to a constant pointer ( (int *const) *ptr
), the pointer itself is not constant, so the result is false
.
const int (*)[1]
is a pointer to an array of constant integers, the pointer itself is not constant, so the result is false
.
const int *[1]
is an array of pointers to constant integers ( const int *ptr[1]
), the elements of the array are constant pointers, so the result is false
.
const int[1][1]
is a two-dimensional array, the elements of which are constants, so the result is true
.