简体   繁体   中英

Java's “public static final Object” in C++

What would be the equivalent of this in C++ of the following snippet. I am in the process converting parts of an java application to C++.

Here is that java class snippet:


class container {
  Public static final Object CONTAINER_FULL = new Object {
     public boolean equals(Object other) {
        // ...
     }
     String toString() {
        // ...
     }
     // ...
  }
  // ...
}

The above class is wrapped in an java interface class "container". The call is ...


public Object fetch_the_object(int at_pos) {
     if (at_pos == MAX) {
        return container.CONTAINER_FULL;
     } 
     // ...
}

What would be the closest equivalent in C++ of that static class and its call?

 class Thing
 {
    public:
      static const OtherThing CONTAINER_FULL;
 };
 const OtherThing Thing::CONTAINER_FULL = blah;

Constant, static, non-integral data types must be defined outside the class body. If you want OtherThing to be anything, change it to

void *

Something like this, perhaps:

struct Container {
    struct Full {
        ...
    };
    static const Full full;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM