Pages: [1]   Go Down
Author Topic: Casting a int as a enum  (Read 8795 times)
0 Members and 1 Guest are viewing this topic.
Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#include "WProgram.h"

enum Color {

  RED,
  GREEN,
  BLUE}

Using it

Code:
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.


Logged

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);.
Logged

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

Yorkshire England
Offline Offline
Sr. Member
****
Karma: 2
Posts: 267
Arduino good init
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?    

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: