This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/37065] New: Virtual function from member constructor and destructor
- From: "HHG01200 at nifty dot ne dot jp" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 9 Aug 2008 01:53:10 -0000
- Subject: [Bug c++/37065] New: Virtual function from member constructor and destructor
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
While member objects are constructed or destructed, the owner object's
overloaded virtual function is called.
It may be a problem because the owner object is not fully constructed at these
stage.
I think it is correct if base class' virtual function is called because the
base object is fully constructed at these stage.
------------------
#include <stdio.h>
class A
{
public:
A() {printf("A::A(), "); v();}
virtual ~A() {printf("A::~A(), "); v();}
virtual void v() {puts("A::v()");}
};
class C
{
public:
A* p;
C(A* a) : p(a) {printf("C::C(), "); p->v();}
~C() {printf("C::~C(), "); p->v();}
};
class B : public A
{
public:
C c;
B() : c(this) {printf("B::B(), "); v();}
~B() {printf("B::~B(), "); v();}
void v() {puts("B::v()");}
};
int main()
{
B b;
return 0;
}
------------------
Actual result:
A::A(), A::v()
C::C(), B::v()
B::B(), B::v()
B::~B(), B::v()
C::~C(), B::v()
A::~A(), A::v()
------------------
(I think) correct result:
A::A(), A::v()
C::C(), A::v()
B::B(), B::v()
B::~B(), B::v()
C::~C(), A::v()
A::~A(), A::v()
--
Summary: Virtual function from member constructor and destructor
Product: gcc
Version: 4.0.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: HHG01200 at nifty dot ne dot jp
GCC build triplet: i686-apple-darwin9
GCC host triplet: i686-apple-darwin9
GCC target triplet: i686-apple-darwin9
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37065