开发者

Java's "public static final Object" in C++

开发者 https://www.devze.com 2022-12-21 22:30 出处:网络
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++.

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 stati开发者_StackOverflow社区c 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;
};
0

精彩评论

暂无评论...
验证码 换一张
取 消