Destructor in a Class

Could someone please take a look at the following code and tell me at which point in the execution of main() does the string "Gonna delete!" get printed to the screen? Does it actually happen only after the closing curly brace of main()?

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Simple
{
private:
	int m_nID;
public:
	Simple(int nID)
	{
		std::cout << "contruct" << "\n";
		m_nID = nID;
	}
	~Simple()
	{
		std::cout << "Gonna delete!\n";
	}
	int get_ID() { return m_nID; }
};

int main()
{
	Simple cSimple(99);
	std::cout << cSimple.get_ID() << "\n";
}
Last edited on
It happens at the point where closing curly brace is in main: at the end of function scope.
OK, thanks!

So it actually happens just before (or at the same time as?!) the last curly brace?

Can anything actually happen after the last curly brace?
So it actually happens just before (or at the same time as?!) the last curly brace?
Curly brace is not a part of the code. It denotes end of block scope. You can say that everything what happens at the end of scope (mainly destructor calls) is happening at the closing bracket

Can anything actually happen after the last curly brace?

I am sure that you meant "after main() finishes". Many things happens: destructors for static objects are called, atexit handlers are executed, runtime library finalizes program execution.
Topic archived. No new replies allowed.