Javier
Javier

Reputation: 12398

Destruction of object in local scope

Suppose that I have the following code:

void foo() {  
 {
    myclass object;
    object.do_something();
 }
 cout<<"hello"<<endl;
}

Is ~myclass() guaranteed to be called by the time the local scope is exited, or might it be called at a later time (such as when the function returns)?

Upvotes: 1

Views: 68

Answers (1)

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272687

Yes, it is guaranteed:

[class.dtor] Destructors are invoked implicitly for ... a constructed object with automatic storage duration when the block in which the object is created exits.

Upvotes: 8

Related Questions