0

I want to access my Class' enum which inherited from it's base class but it gives error.

Says I must use Base::One, not Extended::One.

But another people don't know about the Base class, they just know the Extended class which I published with them.

How can I use Extended::One to access all the base class' enums?

class Base {
    public:
    enum Type {
        One,
        Two
    };
};

class Extended : Base {

};

int main() {
    Extended::One; // ERROR: constant Base::One is inaccessible

    return 0;
}

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
eminfedar
  • 558
  • 3
  • 16
  • 1
    [Very related question about the differences between public, protected and private inheritance](https://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance) – Some programmer dude Sep 23 '19 at 16:07
  • 1
    Missing "public" in inheritance, so voting to close as typo. – Marek R Sep 23 '19 at 16:17

2 Answers2

4

You're accidentally using private inheritance. To fix this, define Extended as follows:

class Extended : public Base {

};
jjramsey
  • 1,131
  • 7
  • 17
1

Type might be public in Base, but Base itself is not a public base class of Extended, so your main function doesn't know about it.

Why not define the enum in the global scope if other classes need to use it?

Bas in het Veld
  • 1,271
  • 10
  • 17