Article View: gnu.g++.bug
Article #22g++ 2.3.1, bug in copy constructor
From: Klamer Schutte
Date: Mon, 02 Nov 1992 19:12
Date: Mon, 02 Nov 1992 19:12
115 lines
2563 bytes
2563 bytes
When calling a function, the default copy constructor for an object is
not proper invoked. Here is the function argument a class (and not a
reference, as common).
Correct behaviour will invoke the copy constructor of the base classes,
and the copy constructor of the individual members. After the function
call the destructor must be called.
g++ only calls the destructor...
The output from g++ 2.3.1 is:
A() 0
C() 0
C(copy( 0 )) 1
A(copy( 0 )) 1
A(copy( 0 )) 2
In funca 2
~A() 2
In funcb 0, 0
~C() 0 ~A() 0
~C() 1 ~A() 1
~C() 0 ~A() 0
and it should be: (sun C++ 2.1 output, ignore different place of ~A() 2)
A() 0
C() 0
A(copy( 0 )) 1
C(copy( 0 )) 1
A(copy( 0 )) 2
In funca 2
A(copy( 0 )) 3
C(copy( 0 )) 2
In funcb 3, 2
~C() 2 ~A() 3
~A() 2
~C() 1 ~A() 1
~C() 0 ~A() 0
BTW: It is better as g++ 2.2.1: This produced:
A() 0
C() 0
A(copy( 0 )) 1
C() 1
A(copy( 0 )) 2
In funca 2
~A() 2
In funcb 0, 0
~C() 0 ~A() 0
~C() 1 ~A() 1
~C() 0 ~A() 0
Which result already has been resulted.
And here is the code:
#include <iostream.h>
struct A {
static int cnt;
int val;
A() : val(cnt++) { cout << "A() " << val << "\n"; }
A(A & copy) : val(cnt++)
{ cout << "A(copy( " << copy.val << " )) " << val << "\n"; }
virtual ~A() { cout << "~A() " << val << '\n'; }
};
int A::cnt = 0;
struct C {
static int cnt;
int val;
C() :val(cnt++) { cout << "C() " << val << "\n"; }
C(C & copy) :val(cnt++)
{ cout << "C(copy( " << copy.val << " )) " << val << "\n"; }
~C() { cout << "~C() " << val << '\t'; }
};
int C::cnt = 0;
struct B : public A {
C c;
};
void
funca(A a)
{
cout << "In funca " << a.val << "\n";
}
void
func(B b)
{
cout << "In funcb " << b.val << ", " << b.c.val << "\n";
}
main()
{
B b;
B bb(b);
funca(b);
func(b);
return 0;
}
Klamer
Klamer Schutte Tel: +31-53-892778 Fax: +31-53-340045
Faculty of electrical engineering -- University of Twente, The Netherlands
preferred: klamer@mi.el.utwente.nl SMTP: klamer@utelmi01.el.utwente.nl
Technical Details
Message-ID:
<9211021712.AA11879@mi.el.utwente.nl>Path:
arc1-ca.pugleaf.net!sparky!uunet!noc.near.net!hri.com!spool.mu.edu!caen!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!mi.eltn.utwente.NL!klamerShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!noc.near.net!hri.com!spool.mu.edu!caen!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!mi.eltn.utwente.NL!klamer From: klamer@mi.eltn.utwente.NL (Klamer Schutte) Newsgroups: gnu.g++.bug Subject: g++ 2.3.1, bug in copy constructor Message-ID: <9211021712.AA11879@mi.el.utwente.nl> Date: 2 Nov 92 19:12:34 GMT Sender: gnulists@ai.mit.edu Distribution: gnu Organization: GNUs Not Usenet Lines: 115 Approved: bug-g++@prep.ai.mit.edu