This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
GCC3.0-release and multiple inheritance
- To: libstdc++ at gcc dot gnu dot org
- Subject: GCC3.0-release and multiple inheritance
- From: Simon Gee <simong at oz dot agile dot tv>
- Date: Thu, 02 Aug 2001 13:06:11 +1000
- Organization: AgileTV Corporation Australia
Hi,
I'm seeing some strange behaviour with multiple inheritance and order of
declaration on a mipsel-linux target and I was wondering if anyone has
come across/recognizes the behaviour I am seeing. Characteristically,
when a method is called from a base pointer which does not appear first
in the inheritance list then the destructor for that class gets called
twice. Following this message is a test case that exhibits this
behaviour (compile with -DWORKS for the correct version) - any
comments/suggestions would be appreciated.
Simon
------------------------
#include <iostream>
using namespace std;
class B;
class D
{
public:
D(B* b) {cerr << "The construct" << endl;};
D(const D& r) {cerr << "The copy" << endl;};
~D() {cerr << "There should only be two destruct calls" << endl;};
};
class A
{
public:
A ();
virtual ~A();
virtual void Amethod (D d) = 0;
protected:
private:
A(const A &right);
A & operator=(const A &right);
};
inline A::~A()
{
}
inline A::A ()
{
}
class B
{
public:
B ();
virtual ~B();
virtual void Bmethod (D state) = 0;
private:
B(const B &right);
B & operator=(const B &right);
};
inline B::B ()
{
}
inline B::~B()
{
}
#ifndef WORKS
class C : public A, public B // DOES NOT WORK
#else
class C : public B, public A // WORKS
#endif
{
public:
C ();
virtual ~C();
void Bmethod (D state);
void Amethod (D d);
private:
C(const C &right);
C & operator=(const C &right);
};
C::C ()
{
}
C::~C()
{
}
void C::Bmethod (D state)
{
return;
}
void C::Amethod (D d)
{
return;
}
using namespace std;
int main(int argc, char *argv[])
{
// If this is C* xxxx = new C(); then correct behaviour is achieved
B* xxxx = new C();
D state(xxxx);
xxxx->Bmethod(state);
cerr <<"After Bmethod in main " << endl;
return 1;
}
------------------------------
EXPECTED OUTPUT:
bash-2.04$ ./a.out
The construct
The copy
There should only be two destruct calls
After Bmethod in main
There should only be two destruct calls
ACTUAL OUTPUT (without -DWORKS):
bash-2.04$ ./a.out
The construct
The copy
There should only be two destruct calls
There should only be two destruct calls
After Bmethod in main
There should only be two destruct calls