f*****y 发帖数: 444 | 1 Q: What will happen in the following code after the function foo() returns?
class base
{
public:
base() { }
~base() { }
};
class derived : public base
{
private:
int *p_pi_values;
public:
derived() : p_pi_values(new int[100]) { }
~derived() { delete [] p_pi_values; }
};
void foo(void)
{
derived *p_derived = new derived();
base *p_base = p_derived;
// Do some other stuff here.
delete p_base;
} |
p********7 发帖数: 549 | 2 我觉得会memory leak,因为解析函数会调用的base的解析,而不是derivedclass的 |
g******d 发帖数: 511 | |
A*********r 发帖数: 564 | 4 agree.
【在 p********7 的大作中提到】 : 我觉得会memory leak,因为解析函数会调用的base的解析,而不是derivedclass的
|
x****k 发帖数: 2932 | 5 memory leak, base::~base() is called when deleting p_base. |
l********n 发帖数: 54 | 6 It is a rule that base class must have a virtual destructor to prevent
problems like this. |
w****a 发帖数: 827 | |
f*****y 发帖数: 444 | 8 memory leak is obvious. but the right answer is "undefined." |
l*******o 发帖数: 791 | 9 When there is inheritance, there must be a virtual destructor in the very
base class. |