Reputation: 820
So I have this namespace called paddleNS for the class called paddle, inside paddleNS I have an enum known as colour
namespace paddleNS
{
enum COLOUR {WHITE = 0, RED = 1, PURPLE = 2, BLUE = 3, GREEN = 4, YELLOW = 5, ORANGE = 6};
}
class Paddle : public Entity
{
private:
paddleNS::COLOUR colour;
public:
void NextColour();
void PreviousColour();
void PaddleColour(paddleNS::COLOUR col) { colour = col; }
};
Now then, what I was wondering is how would I go about creating a function that will return what the colour currently is also is there an easier way to return it in text form instead of a value or am I better of just using a switch to figure out what the colour is?
Upvotes: 16
Views: 80839
Reputation: 41
Hey you can make your function in header that look like this:
enum COLOUR function();
and when you define a function:
enum Paddle::COLOUR Paddle::function(){
// return some variable that hold enum of COLOUR
}
in main.cpp i enter code here
think you can manage it
Upvotes: 4
Reputation: 267
Keep an array of strings where the indix in to this array of strings matches the enum value you are using.
So if you have:
enum COLOUR {WHITE = 0, RED = 1, PURPLE = 2, BLUE = 3, GREEN = 4, YELLOW = 5, ORANGE = 6};
I would then have an array defined:
String colors[] = {white, red, purple, blue, green, yellow, orange}
Then when you have a function return an enum of this type you can just put it into your array and get the correct color in string format.
Upvotes: 1
Reputation: 2509
class Paddle : public Entity
{
// ----
const char* currentColour() const {
switch(couleur)
{
case WHITE:
return "white";
break;
//And so on
}
}
};
Upvotes: 2
Reputation: 227418
Just return the enum by value:
class Paddle : public Entity
{
// as before...
paddleNS::COLOUR currentColour() const { return colour; }
};
Upvotes: 19