Destructor order

eles via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 22 08:45:01 PDT 2014


C++ versions:

	{ //displays ~C~B~A
	    A foo;
	    B bar;
	    C *caz = new C();
	    delete caz;
	}

	std::cout << std::endl;

	{ //displays ~C~B~A
	    std::unique_ptr<A> foo = std::make_unique<A>();
	    std::unique_ptr<B> bar = std::make_unique<B>();
	    C *caz = new C();
	    delete caz;
	}


D version:

	{ //displays ~A~B~C
	    A foo = scoped!(A)();
	    B bar = scoped!(B)();
	    C caz = new C();
	    destroy(caz);
	}

Why the objects are not destroyed in the inverse order of their 
creation? Case in point, destroying foo releases a lock for bar 
and caz.


More information about the Digitalmars-d-learn mailing list