Just because I don't know exactly where to look this up in my c++ book, or on google. How do I actually define some enumerations(in this case { left=1, right=2, top=3, bottom=4 }
) inside a class. I want to be able to pass this enumeration as a parameter to member functions instead of an integer, therefore using the enumeration externally...
Is there a way I can do this, or is there a better way I which I can make the enumeration specific to that class only?
Here's the code that's not working, saying enum mySprite::<unamed> myySprite::side member mySprite "mySprite::side" is not a type name
for some reason:
class mySprite : public sf::Sprite
{
public:
enum{left=1, right=2, top=3, bottom=4} side;
float GetSide(side aSide){
switch(aSide){
// do stuff
}
};
};
You need to define it as a type to use it outside the class and/or strongly-type the parameter. Otherwise it is simply defined as an
int
, its access modifier must also be public:I think this example explains it all:
The simplest change needed to get your code to work is this:
You can also do it this way:
Which means define an anonymous enum type for your
mySprite
class and makeside
an alias effectively accomplishing the same thing as the code above. For terseness only the first enum value needs to be assigned a starting integer. All values that come after that point are understood to be incremented by 1 each time unless you explicitly assign it something else.