[haiku-development] Re: QT for R2

  • From: Axel Dörfler <axeld@xxxxxxxxxxxxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Sat, 24 Mar 2012 23:22:51 +0100

On 24.03.2012 15:24, Adrien Destugues wrote:
I don't really see what's more "right" in Java.

Java : http://docs.oracle.com/javase/6/docs/api/java/util/Vector.html
C++ : http://cplusplus.com/reference/stl/vector/

So, we have in Java multiple methods that do exactly the same thing (add
and addElement and insertElementAt, elementAt and get), and helper
functions for converting to and from an array.

Vector is not really the class you would want to take as an example here -- it's a bit outdated, and can only be considered a leftover.
The equivalent to std::vector would be ArrayList.

Ok, Java has a remove(Object) method to remove an object from the
vector. Note this can't work in C++ because it is not a reference-based
language. So this is a nice feature we can't replicate.

Of course you could do this. You just wouldn't delete it in that case, obviously.

[...]
This is not *that* inconvenient, I don't see how it is possible to make
it simpler without losing flexibility on the condition, and I don't
think the Java equivalent is much shorter. It allows using it.remove()
instead of it = vector.erase(it), but that's about it ?

With the classes you chose you are indeed missing the point of my complaints :-)
Try the above with sets and maps, and you'll see what I mean.

So, most of Java niceness comes from the fact that it uses garbage
collecting and references, and avoids you the need to delete your
objects. This has another range of problems, such as not knowing when
finalize() will be called, if at all. C++ brings more flexibility by
allowing pointers, references and plain objects in a vector (or other
container). Of course, this comes with some syntax complexity, but I
think it's worth it.

You are indeed comparing the languages here, not the API of the classes.
Java:
        Iterator<MyClass> iterator = list.iterator();
        while (iterator.hasNext()) {
                MyClass object = iterator.next();
                ...
        }

I find that much more readable than the corresponding STL code; the STL code looks ancient, but you could have the same API that Java has in C++ as well.

Bye,
   Axel.

Other related posts: