c**********e 发帖数: 2007 | 1 struct X {
virtual ~X() {}
};
class Y : public X {};
int main()
{
X* p = new Y[2];
delete[] p;
return 0;
}
What, if anything, is an error with the sample code above?
a) A "class" cannot inherit from a "struct."
b) It invokes undefined behavior.
c) "Y" requires a destructor.
d) Nothing is wrong. |
E*U 发帖数: 2028 | 2 d
【在 c**********e 的大作中提到】 : struct X { : virtual ~X() {} : }; : class Y : public X {}; : int main() : { : X* p = new Y[2]; : delete[] p; : return 0; : }
|
d****p 发帖数: 685 | 3 It is b.
【在 c**********e 的大作中提到】 : struct X { : virtual ~X() {} : }; : class Y : public X {}; : int main() : { : X* p = new Y[2]; : delete[] p; : return 0; : }
|
r****t 发帖数: 10904 | 4 zkss?
【在 d****p 的大作中提到】 : It is b.
|
t****t 发帖数: 6806 | 5 short answer: polymorphism and array do not mix.
long answer: read effective c++.
【在 r****t 的大作中提到】 : zkss?
|
c**********e 发帖数: 2007 | 6 thrust is right.
b) It invokes undefined behavior.
This is correct. When deleting an array, the dynamic and the static type of
the object must be the same, or the behavior is undefined (C++ Standard 5.3
.5/3).
【在 t****t 的大作中提到】 : short answer: polymorphism and array do not mix. : long answer: read effective c++.
|