Victor Bazarov wrote:
easy wrote: The following code gives me compiler errors galore telling me that
I'm crossing the initalization of the vectors and that case label 2
and default are "within scope of cleanup or variable array". I
suspect its the destructors for the vectors that is causing the
problem but why are they being called at all? Shouldn't they go
out of scope at the closing bracket after default?
Putting brackets around the contents of case1 solves my problem but
I still dont understand why I need to do so.
Add:
{
std::cout << "Testing Scanner DAC" << std::endl;
std::vector<uint32_t> test;
std::vector<uint32_t> result;
Add:
}
I think he understood the "what" of the problem, just not the "why".
The Standard addresses this:
It is possible to transfer into a block, but not in a way that bypasses
declarations with initialization. A program that jumps77) from a point
where a local variable with automatic storage duration is not in scope
to a point where it is in scope is ill-formed unless the variable has
POD type (3.9) and is declared without an initializer (8.5).
__________________
77) The transfer from the condition of a switch statement to a case
label is considered a jump in this respect.
So, if I understand it right, jumping to case label 2 bypasses the
initialization of "test" and "result" but those variables are still in
scope. By placing them in braces, they have a new scope and the
initializations don't matter.
Case labels are just jump points, they don't create scopes.
If "test" and "result" were POD and uninitialized, then it wouldn't
matter and you could have left out the braces.
Brian