enum command.... again

enum again i'm afraid.....

I'm still struggling with the enum command and it's scope(i believe it's called..).
In which file(of the three below) do i declare the following enum command ?

enum weather { rainy,cloudy,snowy };

for my example below. Is it in main()?, if so, will the header file then recognise the weather enum type?.

I assume you only declare it once somewhere ?.As i've already posted..,i've only seen enum work when everything is in one file.

Again, apologies for any syntax or other errors. I hope you can see what i'm trying to say , even if i've not explained it too well.

stormType.h // header for class

class stormType
{
public:
stormType( weather w1); // contruc with 1 parameter of enum type weather

protected:
weather var1;
}

stormType.cpp //implementation file for method definition

stormType::stormType( weather w1 ) { var1 = w1; }


main.cpp // main program

main()
{
stormType s1(rainy);
}
Since it's used in stomrType.h, it would have to be either in stormType.h or in some header that is included by stormType.h. (or in some header that is always #included before stormType.h is included, but I would avoid doing that as it makes you have to remember the right order to include things, which is a maintenance nightmare).

putting it in main.cpp or in main itself would be no good.


Enums are "include dependencies". See section 4 of this article: http://cplusplus.com/forum/articles/10627/#msg49679
I've put the enum commands in the header file but obvously not in the class. Seems to work fine and permeate through the files because the header is called out and used through out code.

didn't think this work work....

there you go...
I think enum can be used in two places. One is in the global namespace which we did in C days.

E.g
enum Weather { rainy, cloudy, snowy }

The other is to put it inside a C++ class
E.g
class stormType {
public:
enum {rainy, cloudy, snowy };
...
}

To reference the C++ enum, we need prefix stormType::rainy stormType::cloudy stormType::snowy.

So if you look carefully, you have "simulated" static constant values for a C++ class.
You can put enums anywhere. The only thing that changes is their scope. You can even have them local to functions... or even to blocks inside of functions.

The same is true for any type in C++. enums aren't special or anything.

It seems like this is just a question of how scope rules apply.
Having listening to what you've all said, I think I'll try something else
Instead. Basically I'm trying to write a program which will involve list of days , weeks,etc.. , and I thought this would be best achieved using enum as the alternative would be using strings to store "Monday","Tuesday",etc instead of integers. I believe I could achieve a similar results by using

#define MONDAY 2

and so on? In an include file?. What do you think?
I think it's a bad idea

#defines pollute all namespaces and break language and scope rules, and can potentially lead to unexpected troubles. You really should avoid them.

enums should be preferred to #define groups in virtually all situations.
Topic archived. No new replies allowed.