What happens when we combine RAII and GOTO ?
Posted
by Robert Gould
on Stack Overflow
See other posts from Stack Overflow
or by Robert Gould
Published on 2010-03-09T04:56:34Z
Indexed on
2010/03/09
5:06 UTC
Read the original article
Hit count: 281
I'm wondering, for no other purpose than pure curiosity (because no one SHOULD EVER write code like this!) about how the behavior of RAII meshes with the use of Goto (lovely idea isn't it).
class Two
{
public:
~Two()
{
printf("2,");
}
};
class Ghost
{
public:
~Ghost()
{
printf(" BOO! ");
}
};
void foo()
{
{
Two t;
printf("1,");
goto JUMP;
}
Ghost g;
JUMP:
printf("3");
}
int main()
{
foo();
}
When running the following code in VS2005 I get the following output:
1,2,3 BOO!
However I imagined, guessed, hoped that 'BOO!' wouldn't actually appear as the Ghost should have never been instantiated (IMHO, because I don't know the actual expected behavior of this code).
Any Guru out there knows what's up?
Just realized that if I instantiate an explicit constructor for Ghost the code doesn't compile...
class Ghost
{
public:
Ghost()
{
printf(" HAHAHA! ");
}
~Ghost()
{
printf(" BOO! ");
}
};
Ah, the mystery ...
© Stack Overflow or respective owner