Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

What is the order of destructor calls with delete and End of main() in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class A {
	char *p;
public:
	A() : p(new char[255]) {}
	~A() { cout << "~A()" << endl; delete[] p; }
};

class B {
	A *Ap;
public:
	B() : Ap(new A()) {}
	~B() { cout << "~B()" << endl;  delete Ap; }
};

class C {
	A Ao;
public:
	C() : Ao() {}
	~C() { cout << "~C()" << endl; }
};

int main()
{
	B *Bp = new B();
	cout << "delete Bp;" << endl;
	delete Bp; 

	C *Cp = new C();
	cout << "delete Cp;" << endl;
	delete Cp;

	B b;
	C c;

	cout << "end main()" << endl;

	return 0;
} 


/*
run:

delete Bp;
~B()
~A()
delete Cp;
~C()
~A()
end main()
~C()
~A()
~B()
~A()

*/

 



answered May 25, 2018 by avibootz

Related questions

...