Can I give an enum member functions, as if it were a class?

Apr 6, 2019 at 3:21pm
let's say we have:
1
2
3
4
5
6
7
8
9
enum class day {
	monday,
	tuesday,
	wednesday,
	thursday,
	friday,
	saturday,
	sunday,
};


can we create a function
 
day::get_char();


which could be called like this
1
2
day d = day::monday;
char c = d.get_char();


This is possible to be done in JAVA is there anything like that in C++


NOTE: the original title of this thread was
"add fucntions to enum class"
Last edited on Apr 6, 2019 at 4:10pm
Apr 6, 2019 at 3:29pm
I think you're asking something like "Does C++ have a way to turn an enum into a string representation of that enum?"

For example, in the above, a way to take day::monday and get the string (not char) "monday".

Is that what you're asking?
Apr 6, 2019 at 3:32pm
@Repeater
No the code provided above is just an example

I may want to associate multiple values/functions on a single enum class

update:
example from java (from https://www.geeksforgeeks.org/enum-in-java/)
note Color is a enum not a class
{
Color c1 = Color.RED;
System.out.println(c1);
c1.colorInfo();
}
Last edited on Apr 6, 2019 at 3:36pm
Apr 6, 2019 at 3:42pm
Scoped enumerations are not classes despite the class keyword being used. You'll have to make it a free function that takes the enum as argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

enum class day
{
	monday,
	tuesday,
	wednesday,
	thursday,
	friday,
	saturday,
	sunday,
};

char get_char(day d)
{
	switch (d)
	{
	case day::monday:
		return 'M';
	case day::tuesday:
		return 'T';
	case day::wednesday:
		return 'W';
	case day::thursday:
		return 'X';
	case day::friday:
		return 'F';
	case day::saturday:
		return 'S';
	case day::sunday:
		return 'Y';
	}
	return '?';
}

int main() 
{
	day d = day::monday;
	char c = get_char(d);
	std::cout << "Character of the day: " << c << '\n';
}
Character of the day: M
Last edited on Apr 6, 2019 at 3:45pm
Apr 6, 2019 at 4:05pm
Ah, I see. The question is "Can I give an enum member functions, as if it were a class?"

As Peter87 says, no.
Apr 6, 2019 at 4:16pm
@Peter87
t@Repeater
thanks

i was just hopping that wasn't the case
Apr 6, 2019 at 4:24pm
It can be simulated. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

struct day {

    enum day_t { monday, tuesday, wednesday, thursday,
                 friday, saturday, sunday };
    day_t d ;

    constexpr day( day_t d = monday ) : d(d) {}

    constexpr operator day_t() const { return d ; }

    constexpr char to_char() const { return "MTWTFSS"[d] ; }

    std::string to_string() const
    {
        static const std::string str[] { "monday", "tuesday", "wednesday", "thursday",
                                         "friday", "saturday", "sunday" };
        return str[d] ;
    }
};

int main() {

    const day today = day::saturday ;
    std::cout << today.to_char() << '\n' // S
              << today.to_string() << '\n' ; // saturday
}

http://coliru.stacked-crooked.com/a/d031ef4d1bc09ce6
Apr 6, 2019 at 10:41pm
you can also do foolishness :)

enum days
{
monday = 'M', // the char m is an integer in c++.
tuesday = 'T',


};

getchar() becomes

ans = (char)(monday);

but this breaks the enum's sequential values if you relied upon that, or wanted to use them as array index, or something, then you can't use this trick.

if you wanted to have distinct T's for tu/th you can do this (even more mad hax)..

tuesday = 'T'*10+1,
thursday = 'T'*10+2,

ans=(char) (anyday/10); //make the integer math work for you. They still won't be sequential or array friendly enums.


Last edited on Apr 6, 2019 at 11:03pm
Topic archived. No new replies allowed.