Do the variables declared inside the if statement seem to leak to the following expression?
I copied, copied some code and, to my surprise, it compiled when it shouldn't have been! I was going to pass the code, and I guessed that I had reached an error before that. Below is a working (?) Program that shows the problem.
The following code fragment calls a function for an object that does not exist:
#include <iostream>
class A {
public:
virtual ~A() {}
};
class B : public A {
public:
void fooB() { std::cout << "fooB\n"; }
};
class C : public A {
public:
void fooC() { std::cout << "fooC\n"; }
};
int main() {
A* a = new C();
if (B* b = dynamic_cast<B*>(a)) {
b->fooB();
} else if (C* c = dynamic_cast<C*>(a)) {
c->fooC();
b->fooB();
}
return 0;
}
It compiles, and the output is this way:
fooC
fooB
This, of course, is wrong. Should I include some warnings to prevent this code from compiling?
Imagine the horrors if I tried to access some member variables or call a virtual function!
Same behavior in both VS2008 and GCC 4.8