4

In Java, you can write a constructor for an enum, e.g.

private MyEnum(String name, int val) {
    ...
}

And then you can write:

public enum MyEnum {
    FIRST("A", 10), SECOND("B", 20), THIRD("C", 30);

    private MyEnum(String name, int val) {
        ...
    }
}

Is there any way you can do a similar thing for a C++ enum class?

cry0genic
  • 544
  • 3
  • 15
  • Short answer: No. Enumerations don't have constructors. – Jesper Juhl Jul 21 '20 at 16:27
  • 1
    Does this answer your question? [How to write a Java-enum-like class with multiple data fields in C++?](https://stackoverflow.com/questions/1965249/how-to-write-a-java-enum-like-class-with-multiple-data-fields-in-c) – underscore_d Jul 21 '20 at 16:29
  • So this maps each enum constant to a class rather than a number? No, you can't do that. The word `class` in `enum class` is misleading, they aren't very different from regular enums. Your best bet is creating a class, and a bunch of global `const` objects of this class. – HolyBlackCat Jul 21 '20 at 16:30
  • enum class is misleading. It's just a scoped enum that prevents implicit conversion to the underlying type and doesn't pollute your namespace. – Eric Jul 21 '20 at 16:31
  • @underscore_d I mean, I guess that works, but it's no longer an enum, so switch statements and casts to int won't work. I think the best solution for me would be something like: `const string names[] = { [(int) MyEnum::FIRST] = "A", ... }` and just look up the relevant values when I need them. – cry0genic Jul 21 '20 at 16:43
  • 1
    @cry0genic Yeah, probably, in a simple case like just needing a string from the int value. However, your question could be read a tad more generally, in which case the other question is still applicable :-) – underscore_d Jul 21 '20 at 16:50

3 Answers3

6

No, C and C++ enums are just a bunch of constants grouped together. C++ enum classes are the same, but to access them you need to add the name of the enum class as a "namespace".

Blindy
  • 65,249
  • 10
  • 91
  • 131
0
#pragma once
#include <afxwin.h>
#include "winnt.h"
#include "winuser.h"

/// <summary>
//  define Style Constants
/// </summary>
enum IS_STYLES {
    TB_TEXT_DEFAULT = 0,
    TB_TEXT_DOUBLE  = 1,
    TB_TEXT_INTEGER = 2,
    TB_TEXT_CAPTION_ABOVE = 4, 
    TB_TEXT_CAPTION_LEFT  = 8
};

class CTextBox
{

public:
    IS_STYLES mStyle;
    CString mString;

    CTextBox();

    CTextBox(
        CString string,
        IS_STYLES style
        );

};

#include "CTextBox.h"
    

CTextBox::CTextBox(
    CString string,
    IS_STYLES Style)
    :mStyle(Style),mString(string)  //init list
{
   // body
}

#pragma once
#include <afxwin.h>
#include "CTextBox.h"

class CMainFrame : public CFrameWnd   
{

private:
    CTextBox mLength;   // member objects
    IS_STYLES mStyle;
protected:

public:
    CObList myBoxes;
    CMainFrame(); // Constructor
};


#include "CMainFrame.h"
#include "CTextBox.h"

CMainFrame::CMainFrame() // CMainFrame Constructor
{
      /* Create a box that's comprised of an enum and string
         CTextBox's constructor
      */
    CTextBox box = CTextBox(IS_STYLES(TB_TEXT_DOUBLE|TB_TEXT_CAPTION_LEFT),
                       "Hello World");

    myBoxes.AddHead((CObject*)box); // add box to a CObList collection.
};
  • How does this answer OP's question? There is no `enum` constructor in the code, and there could not be one since C++ enums don't allow constructors. – dxiv Jan 09 '21 at 07:23
  • Also, it would be better advised to [edit](https://stackoverflow.com/posts/65622469/edit) your previous answer with any needed clarifications, rather than posting it as a different answer. – dxiv Jan 09 '21 at 07:24
  • To, dixv. What you have in my supplied example is a Class that has an Enum member. The Class could be called EnumClass. It's constructor does allow its Sub Classes of have what ever enums you wish. And use those enums in what ever way they wish. If multiple enums are to be loaded then bitwise And and bitwise Or operations can be used as well. – user2489766 Jan 10 '21 at 23:03
  • OP's question was specifically about `enum` constructors, so a "*Class that has an Enum member*" does not answer that question. Besides, it is irrelevant that the member `IS_STYLES mStyle;` is an `enum` in your code. It could be an `int mStyle;`, instead, and you could initialize it using the same syntax from the class constructor, though of course you wouldn't call that an "*int constructor*". – dxiv Jan 10 '21 at 23:19
-2

Yes,you can! Follow below precisely:

  1. declare the enum list (outside of the class)but in the same namespace and naturally give it a name.

  2. in the .h file declare a class with a member value with a type of the defined enum, its name.

  3. also in the .h file create a class overloaded Constructor who's signature includes having the enum type with a default value(also an enum), and must be at the end of the signature and Initialization list.

  4. in the cpp, use the overloaded Constructor that includes the enum, here use the enum value of your choice, from the declared enum list.

  5. also in the cpp, in the Class implementation list, set the Class member's enum value to the enum declared your Constructor's signature(your chosen value).

Enjoy... Not sure how you'd use "more" than 1 value in the Initialization List which would allow you to then be able to use bitwise and, and bitwise or decision making in your Derived / Instantiated Class.

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • 4
    Welcome on SO. You should write the answer as code, so that it is easy to understand and implement. Like this, it is not really helpful. – Oskar Jan 08 '21 at 02:28
  • It's my hope that this concise bit of code is more helpful than the previously submitted directions. William – user2489766 Jan 09 '21 at 06:21