Here is a simple program, that I am pretty sure has undefined behavior, but I want to be certain.
struct A {
int x;
A(int y):x(y) {}
int foo() { return x; }
};
struct B : public A {
B():A(foo()) {}
};
I am assuming that this has undefined behavior because the call to A::foo
occurs before the A
object is constructed and it reads a class variable that is not initialized.
However, if the implementation of A::foo
is changed to { return 0; }
rather than { return x; }
, i.e. the function doesn't access the class members, is it still undefined behavior?
Further question - does anything change if A::foo
is virtual
?