How to share enum between C++ and Java ;)

Note! This is not actual suggestion for proceeding like this during development ;) It’s one of this “fake it till you make it” solutions. So, please, don’t try it at home! ;)

There was this question at Stack Overflow. How to share enums. And you want to share them between C++ and Java. Well, there is one issue here. You can’t base on comments (to trick compiler) as Java and C++ have the same comment schema. How to use comments to make cross language coding? Take a look here.

You can use a nasty hack with “disabling” public keyword in C++ using macro.

#include <stdio.h>

#define public
#include ""
#undef public

int main() {
  A val = a;
  if(val == a) {
  } else {
    printf("Not OK\n");

Then, inside Java class you can define enum

enum A {

And you can use it in Java later on. As you can see above, it is used in C++ as well.

public class B {
  public static void main(String [] arg) {
    A val = A.a;
    if(val == A.a) {
    } else {
      System.out.println("Not OK");

Seems to work fine :)

> javac *.java
> java B
> g++ -o main ./
> ./main