Is using a enum class inside of a class class un-necessary

mreff555

I should start by saying I'm fairly new to OOP. I am trying to write a portable class which will probably be used in multiple programs. The class requires multiple enumerated lists to store information which are only relevant to the class so I was planning on nesting the enum inside the class. My questions are

  • Is there any reason to use an enum class inside a class, or should I just use enum? Just curious. I posted sample code for reference below.

  • Is there any reason why it would be a really bad idea to make enums in a class public?


#include <iostream>

enum class Color {
    red     = 0,
    green   = 1,
    blue    = 2
};

class CodeLibray {
public:
    enum Color2 {
        red     = 0,
        green   = 1,
        blue    = 2
    };
    enum class Color3 {
        red     = 0,
        green   = 1,
        blue    = 2
    };
    Color color;
    Color2 color2; 
    Color3 color3;
}; 

int main(){
    CodeLibray c;
    c.color = Color::red;
    c.color2 = CodeLibray::Color2::blue;
    c.color3 = CodeLibray::Color3::blue;
}
πάντα ῥεῖ
  • Is there any reason to use an enum class inside a class, or should I just use enum? Just curious. I posted sample code for reference below.

It's pretty obvious that the enum values defined inside CodeLibray would become ambigous if you omit the enum class there.

  • Also, If I want enums to be accessible for assigning public member values but I do not want values in the enum to be changed is this possible?

I don't fully understand what you mean. Just a shot in the dark, something like the following?

enum Color2 {
    red     = ::red,
           // ^^ Refer to the enum declaration in the global namespace
    green   = ::green,
    blue    = ::blue
};
enum class Color3 {
    red     = ::red,
    green   = ::green,
    blue    = ::blue
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

c++: enum inside of a class using "enum class"

From Dev

Enum Inside Class

From Dev

Using an enum in a POCO class

From Dev

Replace enum with struct inside a class

From Dev

What's the benefit of using enum embedded inside a class in JAVA?

From Dev

Using a non-class new statement inside an enum creating?

From Dev

What's the benefit of using enum embedded inside a class in JAVA?

From Dev

Using a mixin inside a class

From Dev

using import inside class

From Dev

Using an RtosTimer inside a class

From Dev

Using a mixin inside a class

From Dev

Using decorators inside a class

From Dev

enum class is not a class or namespace

From Dev

Necessary references when using class that is derived

From Dev

Using an Enum as a flag in a class, Python

From Dev

Using an instance of a class inside another class

From Dev

Python Class inside Class using same methods

From Dev

Enum property in parent class should change inside derive class

From Java

Enum inside class (TypeScript definition file)

From Dev

Dynamic initialization of enum class inside template struct

From Dev

Private enum location inside a class in Java

From Dev

c++ enum class inside std::multimap

From Dev

(JAVA Enums) - Anonymous class inside enum constant

From Dev

c++ enum class inside std::multimap

From Dev

how to create a enum property inside a class with typescript?

From Dev

clojure access enum defined inside a java class

From Dev

Override a class inside of a class

From Dev

Lock class inside a class

From Dev

A class inside of a class