Go Down

Topic: Casting a int as a enum (Read 11824 times) previous topic - next topic

EVP

Does anybody know i you can and if so how i do it. I have a enum of colors. I'm using this when calling certain functions of a class. Within then they use switch/case using the various enum's to do what ever they do.
This all works fine the thing is i want to also be able to call those functions but use a int in place of the enum. Basically so i can map a analog input to the enum range and also select it that way.

I cant however find anyway of doing this.

PaulS

Quote
I have a enum of colors.

Please show how this is defined.

Quote
I'm using this when calling certain functions of a class.

Which class?

Quote
the thing is i want to also be able to call those functions but use a int in place of the enum.

Do you have control over the class? The class members should be defined to take ints as input. Then, you could pass an int or an enumerated value.

Quote
Basically so i can map a analog input to the enum range

This should be easy.

Quote
and also select it that way.

"it"? What is "it" that you are trying to select?

EVP

Sorry that was a really bad post. I know, was a bit to tired to copy the code.

Yes i control the class, and i'm sending the enum to it when i call it.

Yes i know i could change the class to take both but i was thinking as an enum's a sort of int anyway i could just cast it. ( if that makes sense) .So i dont have to re wright a load of functions

the enum

Code: [Select]
#include "WProgram.h"

enum Color {

 RED,
 GREEN,
 BLUE}


Using it

Code: [Select]

Color Currant_color;
Currant_color=RED;

// prototype
void ColorSet(Color this_Color); // havent included the function it's self to keep it simple

//calling the function
ColorSet(Currant_color);

// wanted to do something like this to including the mapping

Currant_color=(int)1; // for green
ColorSet(Currant_color);



Hope thats clear enough had to just wright that up, so i've not compiled this it's just to give an idea it basically like that.



EVP

I know it makes no senses to map a analog input to 3 enum's but i've missed a lot out there's over 30 in the real thing.

EVP

I'm thinking ive got it the wrong way round from what you said. If i send a int to the function i can use the enum in it's place when calling the function?. But not the other way round.

PaulS

You need to think in terms of the function being called, not in terms of how to call the function. The function being called could be overloaded:

void Class::SetColor(Color newColor)
{
}

void Class::SetColor(int newColorNum)
{
}

Then, you can call instance.SetColor(White); or instance.SetColor(4);.

EVP

Yes it is overloaded as well. But from what you said with very little change it is now working. I was thinking the wrong way round.

It now work's. Nice one Paul Cheers :)

EVP

I'm basically using this now.

void Class::SetColor(int newColorNum)
{
}

Then, you can call instance.SetColor(White); or instance.SetColor(4);.

Without the enum overloaded version you put. This seems to work if it's sent a enum or a int. Is this because a enum is like a definition of an integer? Is this correct?    


PaulS

Yes, an enum is an int. The converse is not, as you have seen, true.

Go Up