Sign in to follow this  
thedodgeruk

passing enums in funtions ??

Recommended Posts

from what i can tell , enums are the best way for states in a program.


i am writing classes and whould like to pass enums in the arguments and use them as return types as well.

the only problem is that i would have to declare them as a global, is there another better way around this problem.

other wise i would have lots of emums as globals and from what i can tell that would get messy

Share this post


Link to post
Share on other sites

class A
{
public:
enum abc { a, b, c };
};

class B
{
A::abc s;

void use_abc(A::abc e)
{
s = e;
e = A::a;
}
};

Share this post


Link to post
Share on other sites
You can declare an enum in the public interface of a class. I don't know if this is good practice but I do it all the time, as long as the enum relates only to that class.

Share this post


Link to post
Share on other sites
Globals are frowned upon because 100 different pieces of code can modify them and you don't know which code did it.

Enums definitions are just definitions so it's not so bad to make them global. If you feel the enum is independent of any class then go ahead. I could be overlooking some obvious design principle though.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this