Can a class defined locally in a scope, access variables accessible
within that scope e.g. all variables in outer scopes that are visible
in that scope, plus any variables declared in that scope.
I need to understand how feasible is the idea of implementing the
semantics of a Java finally block in C++ by putting cleanup code in the
destructor of a local class and creating a locally-scope object on the
stack?
Cheers,
Andy
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
garth_rockett (50)
|
11/9/2005 2:27:55 PM |
|
ndy wrote:
> Can a class defined locally in a scope, access variables accessible
> within that scope e.g. all variables in outer scopes that are visible
> in that scope, plus any variables declared in that scope.
No. Local classes are not closures.
But you can pass references to your scope variables to your object
which destructor will be called at the end of your scope.
Also, what do you want to do there? Ideally variables in your scope
should cleanup after themselves, and not rely on someone else to do the
cleanup. Emulating "finally" in C++ via RAII is not the always a good
idea.
--
Valentin Samko - http://www.valentinsamko.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Valentin
|
11/9/2005 6:38:37 PM
|
|