TransWikia.com

C++ enum class with constructor

Stack Overflow Asked by cry0genic on October 17, 2020

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?

One Answer

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".

Correct answer by Blindy on October 17, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP