Re: should i learn c?
Jeff Schwab wrote:
SG wrote:
Right. The difference is that in Java you are limited to object
references. Such an object reference can be uninitialized so the
compiler can warn about read access to these references. In C++ you
could ...
(1) create the object on the stack at which point it is considered
initialized by the compiler. This would be a case for late
definition.
Why is this more a case for late definition in C++ than in Java? IOW, =
I
My example was an object of class std::ifstream. Consider you'd like
to create it on the stack (because you don't need it to outlive the
scope) but decide to call ifstream::open at a later time. Then you
can make the mistake of using it without it being fully ("logically")
initialized (i.e. opened).
In Java the only thing you CAN do is defining built-in types or
references. And these references can be uninitialized. My point is:
Instead of
MyObject mob = new MyObject(); // default-init
mob.fullinit();
you would rather write
MyOBject mob;
mob = new MyObject("full init");
if you whish to delay initialization. Here the Java compiler gives an
ERROR (Sun's compiler rejects the code) if there's any read access to
"mob" prior the assignment. So, if you stick with initializing
references only with fully usable objects (i.e. opened streams) early
function scope definitions of object references pose no danger.
(2) use an uninitialized pointer instead. The compiler can warn about
read access of uninitialized pointers. This is similar to the J=
ava
version with references. It's okay to define these pointers ear=
ly.
You're really relying on the compiler at that point, though. If the
compiler doesn't warn, you'll get some awfully nasty bugs.
Sure.
Cheers!
SG