This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
C++ base class constructor not called
- To: gcc-bugs at gcc dot gnu dot org
- Subject: C++ base class constructor not called
- From: Tom Harkness <th at geomec5 dot civil dot soton dot ac dot uk>
- Date: Sun, 30 Jan 2000 12:09:59 +0000 (GMT)
Transcript:
$ g++ -v
Reading specs from /usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/specs
gcc version 2.95.2 19991024 (release)
$ g++ -o bug1 bug1.C
$ bug1
construct 0xbffffcdc
destroy 0x8058728
destroy 0xbffffcdc
Surely the base class constructor should always be called, even if the copy
constructor of the derived class is implicitly defined. Isn't this a bug?
(By explicitly defining an empty copy constructor, B(const B &b) { }, the
problem vanishes.)
#include <iostream>
class A
{
public:
A() { std::cout << "construct " << this << "\n"; }
virtual ~A() { std::cout << "destruct " << this << "\n"; }
};
class B : public A
{
public:
B() { }
B *clone() const { return new B(*this); }
};
int main()
{
B b;
A *bb = b.clone();
delete bb;
return 0;
}