bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Too many destructor calls with call by value


From: Herbert Martin Dietze
Subject: Too many destructor calls with call by value
Date: Thu, 1 Nov 2001 15:19:27 +0000
User-agent: tin/1.4.1-19991201 ("Polish") (UNIX) (Linux/2.4.10 (i686))

Hello,

 in addition to <address@hidden>
I would like to add that the strange behaviour with g++ 2.9.5 
does not only occur if derived classes are involved. Also it
applies to g++ 3.0, too -- is it actually a bug at all?

Here's a short summary again. Consider this code (copy
constructor intentionally commented out):

  #include <iostream>
  using namespace std;

  class A {
  public:
    A (void) { cout << "A()" << endl; }
    //A (A &a) { cout << "A(a)" << endl; }
    ~A (void) { cout << "~A()" << endl; }
    void print (void) { cout << "A::print ()" << endl; }
  };

  class B :public A {
  };

  void foo (A a) { a.print (); }

  int main (void)
  {
    A a;
    cout << "before calling foo(a)" << endl;
    foo (a);
    cout << "after calling foo(a)" << endl;
    B b;
    cout << "before calling foo(b)" << endl;
    foo (b);
    cout << "after calling foo(b)" << endl;
    return 0;
  }

Here's what g++ does:

[2.9.5]

  address@hidden:/tmp % g++-2.95 gnasl.cc 
  address@hidden:/tmp % ./a.out 
  A()
  before calling foo(a)
  A::print ()
  ~A()
  ~A()
  after calling foo(a)
  A()
  before calling foo(b)
  A::print ()
  ~A()
  ~A()
  after calling foo(b)
  ~A()
  ~A()

[3.0]

  address@hidden:/tmp % g++-3.0 gnasl.cc 
  address@hidden:/tmp % ./a.out 
  A()
  before calling foo(a)
  A::print ()
  ~A()
  ~A()
  after calling foo(a)
  A()
  before calling foo(b)
  A::print ()
  ~A()
  ~A()
  after calling foo(b)
  ~A()
  ~A()

Why are there _two_ calls to the destructor when leaving the
`foo()' function?

If I define my own copy constructor the problem does not occur
anymore (of course).

Cheers,

Herbert

-- 
Woher soll ich wissen, was ich denke, bevor ich hoere, was ich sage?
-=-=-                             -=-=-=-=-
  Dipl.Ing. Martin "Herbert" Dietze -=-=- The University of Buckingham -=-=- 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]