P
Prawit Chaivong
Hi All,
There is code here.
------------------------------------------------------------------
class Base{
public:
Base(){}
virtual ~Base(){}
private:
int a;
};
class Derived : public Base{
public:
Derived(){}
virtual ~Derived(){}
private:
int b;
};
int main()
{
Base* pb = new Derived[10];
delete[] pb; // <-- crash HERE.
return 0;
}
-------------------------------------------------------------------
But my question is when I remove keyword 'virtual' out of
destructor(both classes). This program is not crash anymore.
I just want to know why it's not crash if its dtor is not virtual.
By the way, I use gcc 3.2.3.
Thank in advance.
Prawit Chaivong
There is code here.
------------------------------------------------------------------
class Base{
public:
Base(){}
virtual ~Base(){}
private:
int a;
};
class Derived : public Base{
public:
Derived(){}
virtual ~Derived(){}
private:
int b;
};
int main()
{
Base* pb = new Derived[10];
delete[] pb; // <-- crash HERE.
return 0;
}
-------------------------------------------------------------------
pointer. Because size of both are not similar.From code, I know that I shouldn't delete Derived object by its parent
But my question is when I remove keyword 'virtual' out of
destructor(both classes). This program is not crash anymore.
I just want to know why it's not crash if its dtor is not virtual.
By the way, I use gcc 3.2.3.
Thank in advance.
Prawit Chaivong