Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing derived class enum with (unscoped) base class enum having the same name

The following code (also on ideone) looks like it shouldn't compile but it does on MSVC 2008 and GCC 4.8.2

#include<iostream>

struct Base
{
    enum State { ON = 11 , OFF = 22 , STANDBY = 33 };
};

struct Derived : Base
{
    enum State { ON = ON , OFF = OFF };  // Huh?
};

int main()
{
    std::cout << Derived::ON << std::endl;
}

Is it standard compliant?

like image 977
Olumide Avatar asked Jun 01 '26 10:06

Olumide


1 Answers

enum State { ON = ON , OFF = OFF };  // Huh?
                  ^^

At this point derived re-definition is not complete, so ON used would be from Base.

like image 178
ravi Avatar answered Jun 03 '26 01:06

ravi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!