Given this sample:
class Base
{
public:
void foo() {};
};
class Derived : public Base
{
};
int main()
{
Base b;
Derived* d = static_cast<Derived*>(&b);
d->foo();
}
I just have three cases: when void foo()
:
- is member of
Base
, - and when it is member of
Derived
, - and when it is member of
Base
andDerived
.
My questions are:
Does the member access expression
d->foo()
is undefined behavior in all three cases?.If the member access expression is UB, Does the only workaround is to use
dynamic_cast
?