If I have something like the following in a header file, how do I declare a function that returns an enum of type Foo?
enum Foo { BAR, BAZ }; Can I just do something like the following?
Foo testFunc() { return Foo.BAR; } Or do I need to use typedefs or pointers or something?
In C, you must use enum Foo until you provide a typedef for it. And then, when you refer to BAR , you do not use Foo. BAR but just BAR . All enumeration constants share the same namespace (the “ordinary identifiers” namespace, used by functions, variables, etc).
enum is a data type, not a function.
A typedef is a mechanism for declaring an alternative name for a type. An enumerated type is an integer type with an associated set of symbolic constants representing the valid values of that type.
The valueOf() method takes a string and returns an enum constant having the same string name.
In C++, you could use just Foo.
In C, you must use enum Foo until you provide a typedef for it.
And then, when you refer to BAR, you do not use Foo.BAR but just BAR. All enumeration constants share the same namespace (the “ordinary identifiers” namespace, used by functions, variables, etc).
Hence (for C):
enum Foo { BAR, BAZ }; enum Foo testFunc(void) { return BAR; } Or, with a typedef:
typedef enum Foo { BAR, BAZ } Foo; Foo testFunc(void) { return BAR; }
I believe that the individual values in the enum are identifiers in their own right, just use:
enum Foo testFunc(){ return BAR; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With