I've scoured the web looking for any kind of example of using an enum in C++ in a similar way that I use them often in Java, and I couldn't really find much of anything beyond the simple "This is an enum, it has names in it ... next topic...."
The best I can do to describe what Im wanting to do is to show you how I do it in Java - which looks very similar to C++ in this example, and hopefully someone will be able to point out how I can accomplish this kind of an enum in C++.
public enum MOTORS {
MOTOR1,
MOTOR2,
BOTH;
int index = 0;
String getMotorName(MOTORS this) {
switch (this) {
case MOTOR1 -> {return "Motor1";}
case MOTOR2 -> {return "Motor2";}
case BOTH -> {return "Both";}
default -> {return "";}
}
}
public MOTORS next() {
MOTORS response = null;
switch (index) {
case 0 -> {response = BOTH;}
case 1 -> {response = MOTOR1;}
case 2 -> {response = MOTOR2;}
}
index++;
if(index > 2) index = 0;
return response;
}
}
Then the enum can be engaged like this:
public class MotorTesting {
MOTORS activeMotor = MOTORS.BOTH;
private void buttonPressed() {
activeMotor = activeMotor.next(); //changes the variable to be the next MOTORS object based on the index value in the enum
}
private void recordActiveMotor() {
String currentMotor = activeMotor.getMotorName();
//This would return a String with the word "Both" in it unless ButtonPressed() has been executed in which case it would return a String with the word "Motor1" in it.
}
}
Can something like this be done in C++ with enums or something similar perhaps?
It’s kind of an abuse of the class word… an enum class (aka scoped enumeration) creates enumerations that are strongly typed and strongly scoped: as such it does not allow implicit conversion to int, nor allow to compare enumerators from different enumerations, even if they have the same underlying numeric type (that you can provide).
Sure, here is the last one I wrote, but the IDE complains about the declaration of index, and if I put a semicolon after the word BOTH, then it complains about the semicolon.
enum MOTORS {
MOTOR1,
MOTOR2,
BOTH
int index = 0;
String name(MOTOR motor) {
switch case (motor) {
case MOTOR1:
return "Motor1";
case MOTOR2:
return "Motor2";
case BOTH:
return "Both";
}
}
MOTORS get(int num) {
switch(num) {
case 1:
return BOTH;
case 2:
return MOTOR2;
default:
return MOTOR1;
}
}
MOTORS next() {
switch (index) {
case 0:
return BOTH;
case 1:
return MOTOR2;
default:
return MOTOR1;
}
index++;
if(index > 2) index = 0;
}
};
I'm really only going to use the enum to use a button to cycle through the different steppers that I have running so that I can fine-tune my stepping algorithms, and an enum like this makes it really easy to do that and it makes the code simple and easy to read ... where I wont be scratching my head six months from now when I try to figure out what the heck I was doing.
But NOW it's become a dual-purpose pursuit ... I want the knowledge and understanding of how to do this in C++.
@EasyGoing1 this is not valid C++. It's a different language to Java. You can't just cut, paste and hope for the best. If I copied from C++ code into a Java file, that would not compile either.
enum MOTORS {MOTOR1, MOTOR2, BOTH};
struct
{
const char *name;
MOTORS next;
} M[] =
{
{"Motor1", MOTOR2},
{"Motor2", BOTH},
{"Both", MOTOR1}
};
MOTORS activeMotor = BOTH;
void buttonPressed()
{
activeMotor = M[activeMotor].next; //changes the variable to be the next MOTORS object based on the index value in the enum
}
void recordActiveMotor()
{
String currentMotor = M[activeMotor].name;
//This would return a String with the word "Both" in it unless ButtonPressed() has been executed in which case it would return a String with the word "Motor1" in it.
}
void setup() {}
void loop() {}
It is rather insulting to suggest that I am trying to cut and paste code here especially between two different languages. Does it not make sense to you that since I primarily develop in Java, that I might try to use concepts that I use in Java, while trying to adapt them over to C++ ...
And thank you for pointing out that they are different languages ... Seems like it would only be rational for me to create a forum topic on a specific problem so that I can learn how to accomplish my goal using the language I need to accomplish it in ... so then asking people who have more knowledge than I do in the target language ... would be a good course of action, do you not agree?
Your statement above paints me as some amateur that is just trying to hammer a block through a circle. The fact that I even created this post makes your comment assumptive and flat out rude. You should develop better people skills. They matter when you're dealing with PEOPLE.
And in Java we simply preface the word class with public, private or no word at all which makes the class only accessible to other classes which exist in the same package (folder) as the declaring class.