This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]