-
Question about scope...
Working through "Thinking in c++"... and yes I think I'm understanding it this time, thanks to everyone's help, the other day.
One thing is not terribly clear...
Code:
void Fred (int x)
{ // a bunch of code
if (x = 3)
{ Foo bar(31); // class
// calls to bar
} <--- is bar destroyed here?
// a bunch more code
} <---or here?
And ... can I do this to control bar's lifetime?
Code:
void Fred (int x)
{ // a bunch of code
{ Foo bar(31); // class
// calls to bar
}
// a bunch more code
}
-
The scope always ends at the matching closing brace of the opening brace containing the object in question.
And yes, you can create scopes just by having open/close braces in the middle of the code. It's the syntactic equivalent of
Code:
void Fred (int x)
{ // a bunch of code
if ( 1 ) { Foo bar(31); // class
// calls to bar
}
// a bunch more code
}
-
Just for practicing purposes, you can write the destructor that print some message so that when your class goes out of scope you can see your class's destructor called.
-
Thanks guys. Much appreciated.