DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

scoped enum c++11

scoped enumerations

It is new feature in c++11 and it adds the “class” keyword after “enum” keyword.

Example: enum color{red, blue,green};   –>  enum class color{red, blue,green};

Now question is What is the problem with existing one ?

Problem: We cannot use the same name for declaration of variable that is already used in enum due to same scope.

enum color{red, blue, green};

int red = 5;

//error: ‘int red’ redeclared as different kind of symbol

Solution:

Use scoped enum (Its similar like namespace, resolving name conflicts )

source code with example:

#include<iostream>
//New Scoped enum is added
enum class color{red, blue, green};
int red = 5;
int main()
{
std::cout<<"In Enum"<<std::endl;
std::cout<<"red"<<red<<std::endl;
return 0;
}

Output:

In Enum
red:5

Primary Sidebar




DigestCPP © 2023. All rights reserved.

    About Privacy Policy Terms and Conditions Contact Us Disclaimer