L
LAvoisieR
Following test code behaviour suprised me and I dont know what's wrong
with this. I have two overloaded constructors within base class and
virtual destructor implemented. Derived class uses protected
non-parametrized constructor of base class which DOES NOT initialize
private member 'value'. But by calling both destructors virtually,
binary does not complaint about deleting uninitialized pointer. WHY ??
C++ compiler used: G++ 3.4.3 #include <iostream> using namespace std;
class Base { public: Base(int) : value( new int(-14) ) {}
virtual ~Base() { //cout << *value << endl; // throws
SEGFAULT as supposed delete value; // no SEGFAULT
here ?? WHY ?? } protected: /* Constructor overload for
derived classes */ Base() { // does not
initialize 'value' pointer !!! } private: int *value;
}; class Derived : public Base { public: Derived() : Base() {}
virtual ~Derived() {} }; int main() { Base *p =
new Derived; delete p; }
with this. I have two overloaded constructors within base class and
virtual destructor implemented. Derived class uses protected
non-parametrized constructor of base class which DOES NOT initialize
private member 'value'. But by calling both destructors virtually,
binary does not complaint about deleting uninitialized pointer. WHY ??
C++ compiler used: G++ 3.4.3 #include <iostream> using namespace std;
class Base { public: Base(int) : value( new int(-14) ) {}
virtual ~Base() { //cout << *value << endl; // throws
SEGFAULT as supposed delete value; // no SEGFAULT
here ?? WHY ?? } protected: /* Constructor overload for
derived classes */ Base() { // does not
initialize 'value' pointer !!! } private: int *value;
}; class Derived : public Base { public: Derived() : Base() {}
virtual ~Derived() {} }; int main() { Base *p =
new Derived; delete p; }