This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: ordering of constructors
- From: Chris Lattner <sabre at nondot dot org>
- To: "Jay Freeman (saurik)" <saurik at saurik dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 9 Apr 2004 12:26:41 -0500 (CDT)
- Subject: Re: ordering of constructors
> So in this case, the output should be:
> main()
> ~B()
> ~A()
No, I don't think it should be. Consider this modified program, which
just prints out the order of construction as well:
------------
#include <stdio.h>
struct A {
A() { printf("A()\n"); }
~A() { printf("~A()\n"); }
};
A &a() {
static A value;
return value;
}
struct B {
B() { printf("B()\n"); a(); }
~B() { printf("~B()\n"); }
};
B b;
int main() {
printf("main()\n");
return 0;
}
--------------
This prints:
B()
A()
main()
~A()
~B()
... which is what I would expect. The order of destruction has to be the
opposite of the order of construction.
-Chris
--
http://llvm.cs.uiuc.edu/
http://www.nondot.org/~sabre/Projects/