Hey, so I'm working on a personal project that needs to expand well and work as a base for a lot of other possible programs. I've organized several classes, but realized that there's a few unrelated functions and constants many of them share. Given that several of the classes are needing to be stored as objects in a collection, I've decided those functions need to be accessible globally (instead of copied endlessly).
The functions and constants aren't strongly related to each other, so they don't 'fit' nicely in a class (or as a specific 'type'). My solution then was to put the constants, functions, and enums in a header file that defines a namespace for them. The namespace mostly is used to make the usage of those functions/constants explicitly clear in my code, and to keep them all in one place.
So my question is, is this an acceptable approach? Is there a better alternative?
And my question on the enum class:
Within that namespace, are all of my enums... and there's going to be a lot of them. What I need from those enum classes, is to be able to easily convert them back and forth to strings, to define their 'type,' and to be able to compare enums of the same type to each other as ints.
I've looked at both the standard C enum versus the enum class. Given how their type is really important, I'm thinking the enum class is the way to go. So what's the best approach to representing them as strings? Currently I've been using a switch case, but that seems like an excessive way to go about it given that I'll likely have at least 50ish or so enums to do this with. Is there a better alternative?
Secondly, I need to compare them as ints. I understand I'll have to write a function to explicitly convert them. What I'm wondering is, does the same short-cut used with the C enum convention for assigning consecutive int values work for the enum class? Something like this:
1 2 3
|
enum class names{
FIRST_THING = 0, SECOND_THING, THIRD_THING
}
|