'When class A has protected variables, class B inherits from A and class C inherits from B, how do you access the protected variables from class A in C
Sorry about the wordy title, I don't know how to phrase it more concisely.
The error I get is 'identifier "x" is undefined'
I tried doing class C : public B, public A but that didn't seem to work.
My code looks like this at the moment:
class A
{
protected:
float x;
};
class B : public A
{};
class C : public B
{
float y = 1 + x;
// Error: identifier "x" is undefined
};
I can access the protected variables with no problems in class B, I only get the error when I try to access them in class C.
Solution 1:[1]
When class A has protected variables, class B inherits from A and class C inherits from B, how do you access the protected variables from class A in C
You access them in the same way as you would access them in B (so long as the inheritance isn't private). There is no difference with how long the inheritance chain is.
The example program is well-defined since C++11 where default member initialisers were introduced.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
