I had a requirement wherein I have an enum in native C++ code as [all pseudocode]:
enum Dummy {
A,
B,
MAX,
};
Now I also want to have an equivalent enum in Java code
public static enum Dummy {
A,
B,
MAX
};
Doubly defining makes things extremely brittle and error prone, subject to bouts of amnesia or indolence on part of developers. Is there some concrete way to always have them in sync. I don't want a bunch of #defines as suggested by this question.
Go a hard, preprocessor, way ;)
/* main.cc */
#include <stdio.h>
#define public
#include "A.java"
;
#undef public
int main() {
A val = a;
if(val == a) {
printf("OK\n");
} else {
printf("Not OK\n");
}
}
Java code
/* A.java */
public
enum A {
a,
b
}
We can use A enum in Java
/* B.java */
public class B {
public static void main(String [] arg) {
A val = A.a;
if(val == A.a) {
System.out.println("OK");
} else {
System.out.println("Not OK");
}
}
}
And execute
> javac *.java
> java B
OK
> g++ -o main ./main.cc
> ./main
OK
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With