Consider the following code:
void foo()
{
{
CSomeClass bar;
// Some code here...
goto label;
// and here...
}
label:
// and here...
}
Will the destructor of bar be called ?
Consider the following code:
void foo()
{
{
CSomeClass bar;
// Some code here...
goto label;
// and here...
}
label:
// and here...
}
Will the destructor of bar be called ?
1) Yes. 2) Don't do this.
Elaboration: conceptually, this is no different from leaving a loop via a break
. goto
, however, is strongly, strongly discouraged. It is almost never necessary to use goto
, and any use should be scrutinized to find out what's going on.
Yes, they will be called.
Update: (it's okay to do this, gotos is not worse than throwing dummy exceptions or using bools/ifs to get out of things. A simple goto inside a function don't make it spaghetti code.)
The C++ Standard says:
On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2) (named objects or temporaries) that are declared in that scope, in the reverse order of their declaration.
So the answer is "yes".