My question is this: why does the following code:
class A
{
public:
A()
{
test[0] = "three";
test[5] = "five";
test2[3] = 5.55;
}
void foo(int j)
{
for(int i = j+1; i <= 7; ++i)
{
try
{
std::cout<<test.at(i)<<"\n";
}
catch(const std::out_of_range&)
{
try
{
std::cout<<test2.at(i)<<"\n";
}
catch(const std::out_of_range&)
{
throw i;
}
}
}
}
virtual void bar()
{
}
std::map< int, float > test2;
std::map<int, std::string> test;
};
class B : public A
{
public:
B()
{
test3[6] = 15;
test3[7] = 42;
bar();
}
void bar()
{
int k = -1;
label:
try
{
foo(k);
}
catch(int i)
{
try
{
std::cout<<test3.at(i)<<"\n";
}
catch(const std::out_of_range&)
{
k = i;
goto label;
}
}
}
std::map<int, int> test3;
};
three
5.55
five
15
and not
three
5.55
five
15
42
?
What I’m trying to do is iterate over a number of maps containing different data types that can’t be held in 1 container and this is what I came up with
My understanding is that what you need is:
Instead of this convoluted exception+goto design, why not use a simpler design based on a virtual method for printing a specific value: