Post by James KanzePost by Roland PibingerIt's not so simple to 'port' the core Java API to C++ as it
seams at first sight.
It depends on what you mean by "port". Just mimicing the
Java API exactly, mapping what Java calls references to C++
pointers, is really a problem, but it is stupid, even if you're
using garbage collection. The languages are different, and are
(or should be) used differently.
IMO, it's possible to 'transfer' the core Java API to C++ if you
explicitly address the differences between the two languages in your
design. Besides better usability the advantage would be that nowadays
more people know the Java API compared to those who know the C++ std
library (even among C++ programmers ;-)
Post by James KanzePost by Roland PibingerJava e.g. uses references to objects. The 'copy' of a
container doesn't copy objects, only references.
Do Java containers support copy? Java doesn't have copy
constructors. Java doesn't need or want copy constructors,
Right, nobody needs them in Java (although Java has copy constructors
and the clone function) and nobody needs them in C++. In Java, C++ and
other languages objects in the OOP sense are non-copyable, at least
conceptually. But in C++ you cannot e.g. just return a shallow copy of
a container of pointers from a function as you would do in Java.
Post by James Kanzebecause Java uses reference semantics, not value semantics.
You can do this in C++ too, but it's stupid. It doesn't really
fit into the language. (And again, this is true even if you use
garbage collection.) What you want to do is to "adapt" the Java
API to C++, taking what's good in it, and adapting it to the C++
idiom.
That's the point. Adapt the Java API to C++ idioms. You can do the
same in C++ with pointers what you can do with references in Java. As
long as you don't try to copy the container the differences are minor.
Of course, in C++ you always have to take ownership into
consideration.
Post by James Kanze(For example, considered globally, Swing isn't bad at
all---it seems significantly better than the C++ GUI libraries
I've looked at. And adapting Swing to the C++ idiom could even
fix some of the minor flaws it has, e.g. by returning Dimension
as a value, rather than as a reference.)
I wouldn't consider Swing a 'core' Java API (and many GUI experts
wouldn't agree with your assessment of Swing).
Post by James KanzeFor other things, you might not want to adopt Java directly.
The Java collections may be better than the C++ containers
(which a probably about the worst I've seen, from a design point
of view),
STL was meant as a proof of concept. It would have needed some
iterations in the real world to become more user-friendly. The current
'value only' approach in STL is unnecessary limiting but its
functional programming concepts are more fashionable today than they
were in 1995.
Post by James Kanzebut they still are far from known best practice.
There are libraries available (e.g. OSE) which are better than
both.
Maybe, but familiarity is the 'selling proposition' for the Java API.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch