Hi, Marc On Mon, 27 Sep 1999, Marc Hemberger wrote: > Dear Rooters, > > (no ROOT specific question, only C++) It should not be posted here then... Use one of the gcc-related mail-lists. Nevertheless... Your question was about why gcc generates 3 objects of class Except even it is enough to have only two objects in your test program. > I have the following problem (not really a problem, but I'd like to > understand, what's going on). Compiling the program as follows on AIX 4.3 > with xlC_r, I get a very different result than on Linux with egcs: > > #include <== different includes for AIX/Linux #include <iostream> // In accordance with C++ Standard. Works with gcc. > > class Except > { > public: > Except() { cout << "Except constructor\n"; } > Except(const Except &e) { cout << "Except copy constructor\n"; } > ~Except() { cout << "Except destructor\n"; } > void fun() { cout << "Except fun\n"; } > }; > > int main() > { > > try > { > throw Except(); I belive that at this point gcc calls constructor for Except and THEN passes this object (created via default constructor) to 'try-catch' block via copy constractor. The original object is deleted after the copying. So at this line it is called default ctor 1 time, copy ctor 1 time, dtor 1 time. At this point one object of class Except is useless. Please, investigate page http://gcc.gnu.org and write to appropriate mail list for detailed information. > } > > catch(Except x) Ok, the copy constructor will be called for 'x' object. And it will not be called if you use catch(Except &x) Copy ctor is called 1 time. > { > cout << "Caught exception Except" << endl; > x.fun(); > } > catch(...) > { > cout << "Caught undefined exception " << endl; > } > > return 0; > } And finaly, 2 times Except::~Except() dtor is called. > > > On AIX, the result is On Linux I get > (as I would expect it): (what I don't understand): > Except constructor Except constructor > Except copy constructor Except copy constructor > Caught exception Except Except destructor > Except fun Except copy constructor > Except destructor Caught exception Except > Except destructor Except fun > Except destructor > Except destructor > > Can anybody explain me, why on Linux the copy ctor is called in first > place and then followed by a dtor? My suspicion is that it has something > to do with the handling of local scopes in the egcs compiler on Linux. Ha > somebaody any experience with that? > > ... P.S. Your example works (with generation of three objects) on gcc-2.95.1. With best wishes, Alexander Zvyagin.
This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:40 MET