This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

C++ base class constructor not called


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;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]