This is a new feature in C++11.
enums got rid of the need for integer #define constants, but still had some serious issues(like it allowed to compare with other enum types, which is meaningless).
So strongly typed enums (enum class) are introduced in C++11. The use of the word class is meant to indicate that each enum type really is different and not comparable to other enum types.
Enum classes have advantages like better scoping and this feature for forward declaration that you mentioned. It is not the same as the class in your link.
Why is this useful???
Forward declarations are often about the
physical layout of code on disk into different files or to provide
opaque objects as part of an API. In the first case, where you care
about the physical disk layout, using a forward declaration allows you
to declare an enum type in the header file while putting specific
values into the cpp file. This lets you change the list of possible
enum values quite frequently without forcing all dependent files to
recompile. In the second case, an enum class can be exposed as a
type-safe but otherwise opaque value returned from one API function to
be passed into another API function. The code using the API need not
know the possible values the type can take on. Since the compiler
still knows about the type, it can enforce that variables declared to
work with that type are not confused with variables working with
another type.
Refer here.
Following is an example:
enum class myStronglyTypedFloatEnum,myStronglyTypedCharEnum;
void myFunction(myStronglyTypedFloatEnum f,myStronglyTypedCharEnum c);
enum class myStronglyTypedFloatEnum : float {....}
enum class myStronglyTypedCharEnum : char {....}
This is of course meaningless as they are incomparable:
if(myStronglyTypedFloatEnum::f == myStronglyTypedCharEnum::c) //NOT ALLOWED
Another example:
#include <iostream>
using namespace std;
enum class fruit;
void printFruit(fruit f); //Allowed :D
enum class fruit: int {MANGO = 1,APPLE = 2};
void printFruit(fruit f)
{
if(f == fruit::MANGO) //f == 1 will give error: no match for ‘operator==’ (operand
//types are ‘fruit’ and ‘int’)
cout<<"fruit is mango";
else
cout<<"fruit is apple";
}
int main() {
fruit myfruit=fruit::MANGO;
printFruit(myfruit);
return 0;
}