Notice that the Derived class constructor has ii as its first argument, but the argument passed to Base was made equal to i on purpose.
class Base
{
protected:
int i;
public:
Base(int i) : i(i) {}
};
class Derived : public Base
{
private:
int k;
public:
Derived(int ii, int k) : Base(i), k(k) {} // Why not C2065: 'i' undeclared identifier
};
int main()
{
}
Because
iis a member variable inherited fromBase, so it is defined. You can freely access member variables in initialiser lists, but what you’re doing is accessing a variable before it’s initialised, which is, I believe, Undefined Behaviour.