views:

160

answers:

2

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?

A: 
 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 *
Zach
Their definitions should also generally be in implementation file (.cc, .cpp) rather than in the header.
Tronic
Yes, indeed Tronic. The example here is with brevity in mind, but would certainly work.
Zach
And the toString Function would be implemented as an C++ operator<< checking for that object?
Andreas W. Wylach
A: 

Something like this, perhaps:

struct Container {
    struct Full {
        ...
    };
    static const Full full;
};
Tronic
I think in C++ it would be somekind like a wrapper class arround that array (see my commeent above). Everytime a NULL (end of sentnce) is reached, that object is returned. CONTAINER_FULL in string representation (or that toString method) is "end". But I am not sure yet ....
Andreas W. Wylach