Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!snorkelwacker!bloom-beacon!athena.mit.edu!mleblanc From: mleblanc@athena.mit.edu (Marc LeBlanc) Newsgroups: comp.lang.c++ Subject: PolyMorphism Message-ID: <1990Apr10.112351.24301@athena.mit.edu> Date: 10 Apr 90 11:23:51 GMT Sender: news@athena.mit.edu (News system) Organization: Massachusetts Institute of Technology Lines: 47 > If we use a Base pointer to construct a Derived object, will the >Derived destructer be used when been deleted? I have used the >following code to explore: >-------------------------- >// test derived constructer and destructer >#include >class Base >{ > public: > Base() { cout << "This is base constructer\n";}; > ~Base() { cout << "This is base destructer\n";}; >}; >class Derived : public Base >{ > public: > Derived() { cout << "This is derived constructer\n";}; > ~Derived() { cout << "This is derived destructer\n";}; >}; >main() >{ > Base* test; > test = new Derived; > delete test; >} >---------------------------- >And the output as follow: >This is base constructer >This is derived constructer >This is base destructer >So the derived destructer won't be called if we construct the Derived >object by Base pointer. But isn't it more natural to call both? If I >misunderstood the C++, is there anyway you can call Derived class >destructer? I think this would qualify as an "undocumented feature." If you declare the base destructor as virtual, you get the desired effect. (I tried it). Shouldn't this be the default? ------------------------------------------------------------------------ Marc LeBlanc mleblanc@athena.mit.edu