Let’s say I’m building a simple class that prints some text on the screen, and it has the possibility to change the colors of the text.
myclass a("this is the text");
a.setColor("green");
I’m still learning C++ and I recently got introduced to enum and thought I’d give it a try. I was wondering if it is a good practice to use enum types in interface functions like the above setColor?
What are the advantages or disadvantages of using enum classes in interface functions? Are there cases where they are more applicable than and are there case where they are bad to use?
What if I want to combine properties? E.g.
a.setAttribute("bold reverse");
I don’t know if interface is the correct term for what I want to describe: the functions that a user of my class would end up using.
In your case, there are (at least) two advantages:
switchstatement.One potential “disadvantage” is in the case where the colour string has come from e.g. run-time userinput (they’ve typed into a textbox or something). You will need to parse this string and convert it into an enum. But it’s not really a disadvantage, because you’ll need to do this anyway. Best practice is for the user-interface logic to validate the string and convert to the enum at the earliest opportunity.
I can think of at least three options:
setAttribute.|.