Trouble with using protected enum class from base class as public enum class in derived class [MSVC 2019]

Viewed 129

We're having a situation like below.

We're trying to access a protected nested enum class from outside the class that contains it, through a derived class by declaring this same enumeration public in the derived class through the use of the using keyword.

We're using the MSVC2019 compiler and the below code doesn't compile. It results in a C2248 error stating that we cannot access the protected enum. I've tried with GCC as well, and then it seems to compile just fine, like I expected it to.

I was hoping there's someone out there that might be able to explain on this?

Not looking for a workaround though, that we already found.

#include <iostream>

class Base
{
protected:
  enum class Field
  {
    A,
    B
  };

};

class Derived :
  public Base
{
public:
  using Base::Field;

  Derived(Field f);
  
};

Derived::Derived(Field f) {  
  switch (f)
  {
  case Field::A:
    std::cout << "A" << std::endl;
    break;
    
  case Field::B:
    std::cout << "B" << std::endl;
    break;
  }
}

int main()
{
  Derived d(Derived::Field::B);

  return 0;
}
0 Answers
Related