Let’s say one has a class Foo such as
class Foo {
public:
void bar();
operator bool() const { return true; }
};
then one can do
if(Foo foo = Foo())
{
if(Foo foo = Foo())
{
foo.bar();
}
}
Now I’m having trouble grasping the scope resolution going on here (I would’ve expected a compiler error for redeclaring foo).
I expect foo.bar() to execute on the second foo (its scope is “closer”) but am I garanteed that it’s actually a different object than the first foo? Furthermore, are they each independently disposed (their destructor called) at the end of their respective if blocks?
C++ is quite happy for you to declare a variable with the same name, as long as it is inside a nested scope so there is no ambiguity.
You are correct
Yes
Yes