Sign in to follow this  
Silvo

passing enumerations

Recommended Posts

Another question for the ever-reliable For Beginners helpers... Is it possible to, and how can I, pass enumerations to functions/classes. To expand, suppose I have a class that contains a std::vector, but also had an empty enum, like so:
class myExample
{
  public:
    enum theEnum { };
};

would it be possible to create an enum elsewhere, give this new enum some values, and assign the empty enum with this new one (so that the values in the new enum are put into the previously empty enum:
int main()
{
  myExample eg;
  enum newEnum { Zero, One, Two, Three };
  eg.itsEnum = newEnum;

/* or:
 * enum newEnum { Zero, One, Two, Three };
 * newEnum newEnumInstance;
 * eg.itsEnum = newEnumInstance;
 */

Of cause, this is simple an idea of what it *could* look like, I will probably need some sort of function or something... Any ideas on how something like this would work. I want to create an animation, however there could be several animations in that (eg. Walking, running, hopping, etc), and Id like to be able to assign these to it, if you know wht I mean... Anyway, any ideas?

Share this post


Link to post
Share on other sites
Enums doesn't work like that. They're a list of values known at compile time

What you can do is


enum MyEnum
{
Zero,
One,
Two
};

class myExample
{
public:
MyEnum m_Enum;
};

int main()
{
myExample eg;
eg.m_Enum = One;
}



As you can see, and "instanciated" enum represents a value from the list declared in the enum.

Hope this clears it up

Share this post


Link to post
Share on other sites
thanks. Although, can you think of another way in which I could dynamically create a list of "states". Is that the right terminology? Maybe a vector of strings... hmm.

Share this post


Link to post
Share on other sites
I don't see what the vector of strings would do...

If you wanna have say, a "walk", "run", "jump" list of animations, why not using a std::map<std::string, AnimationClip*>? (I'm suggesting std::string but in real, it would be better using an enum if all your clips are known at compile time or a generated id otherwise)

Share this post


Link to post
Share on other sites
Enums are viewed the same as consts in the class, you can access them by using the class. Class::Emun (c++) or Class.Enum (C#). Do not use your instaniation of the class but the class name itself.

theTroll

Share this post


Link to post
Share on other sites
Just a quick note...

If you place enumerations above the class as per the previous example you do not need to qualify it via the class. That being said, it's my understanding this is fairly poor practice as it creates 'namespace polution'. A better solution might be to include it in the class:



class Test
{
public:

enum myEnum {Zero = 0, One, Two, Three};
//This could also be private, but then you're limited to using it within the class

void select(myEnum pick) //body should be in .cpp
{
switch(pick)
{
case Zero : std::cout << "You picked Zero";
break;
case One : std::cout << "You picked One";
break;
case Two : std::cout << "You picked Two";
break;
case Three: std::cout << "You picked Three";
break;
default : std::cerr << "This should never get called!";
}
}

int main()
{
Test newTest;
newTest.select(Test::One);
return 0;
}




Returns "You picked One"... hope this helps a bit

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