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 i
is a member variable inherited from Base
, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With