Ian T wrote:
> Rhino wrote:
>> I realize that this is not entirely a Java question but I am hoping that
>> some of the people reading this newsgroup are Java programmers who
>> went on
>> to learn C++.
>> I am giving some thought to applying for some jobs that want people with
>> Java and C++ experience. I have been writing Java for several years
>> and am
>> fluent enough that I don't have to post questions here very often. I
>> have no
>> real C++ experience and not much C experience for that matter.
>> However, the core Java statements are "borrowed" from C and C++ has
often
>> been called "C with classes".
> Having programmed C++ for ~3years and now learning Java, I can say that
> C++ (and C ) has some nasties that takes years to completely get your
> head around.
> First of all: memory management. You've got to follow that object
> reference (and mallocs) everywhere it goes and anticipate every
> situation where it might be stranded. A good bounds checker will help,
> but still, it's something that you never think about with Java (garbage
> collector), but you should always be thinking about in C++.
I've never found memory management to be a great problem in C++. And in
the doubtful cases, you use some sort of smart pointer. It's a far cry
from having garbage collection, and it IS one more thing you have to
think about, but since at the conceptual level, you have to be concerned
with lifetime of object issues anyway, it usually ends up to be just one
more implementation detail. A bit of extra work, but not something
difficult to get your head around.
> Second: Pointers. References (&), de-references(->), points(*().),
> pointer(*), pointer arithmetic, char arrarys, memory buffers, and so on.
> Learning pointers is the hardest part of C++, and once you have a good
> handle on that, some of the other things come easier too.
Pratically, except for the absence of garbage collection, I don't seem
much difference between C++ pointers and Java references. You can do
more things with C++ pointers, and if you do, it can become difficult,
but you don't normally worry about things like pointer arithmetic unless
you are writing low level software, like a garbage collector.
> Third: Null terminated character arrays. Useful, but often dangerous as
> you can kill the null terminator and have string functions wander off
> into other parts of the stack or the heap. Also, C style string
> functions are the source of many buffer overflow exploits. For most
> string handling <basic string> is your friend, but null terminated
> character arrays have enormous flexibility.
It's been ages (something like fifteen years) since I've used a null
terminated character string in C++ other than at a C interface. In
fact, if someone starts talking about null terminated character strings
in C++, I pretty much take it as a sign that he doesn't know C++.
> Fourth: Learn the containers in STL as soon as practically possible,
> especially <map> and <list>.
Regretfully, I'll agree. IMHO, the library is a disaster in C++, but it
is very *in* to vault the STL. In practice, I'd say that you do have to
know the tradeoffs with regards to the containers (vector, deque and map
are the ones I use most often -- almost never list). On the other hand,
I've found very little use for most of the algorithms in practical code.
> >It seems to me that it shouldn't take very
> > long to get up to speed on C++ if I am already fluent with Java
and have at
> > least some knowledge of C.
> Good luck with that
. Probably *the* best book (IMNSHO) for starting
> out with C++ is Dietel & Deitel C++ How to Program. It's as dense as a
> chocolate pudding, but it has all the bits.
Once the basic syntax is mastered, I'd strongly recommend Scott Meyers'
three books. The first one ("Effective C++"), particularly, addresses a
number of classical issues that you just have to know.
Don't forget that C++ makes intensive use of value semantics. Which
means that you have to decide whether your class should have value
semantics or not, up front. And a class implemented with value
semantics doesn't look like one with reference semantics -- one with
reference semantics often ends up looking very much like a class in Java
(except that we generally declare the copy constructor and the
assignment operator private, so that it doesn't accidentally get used as
if it has value semantics); a class with reference semantics is a horse
of a different color -- you have to understand the difference between
initialization and assignment, for example.
--
James Kanze home:
www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34