Re: On Java and C++
In comp.lang.java.advocacy, Mishagam
<noemail@provider.com>
wrote
on Fri, 28 Apr 2006 15:59:33 GMT
<FZq4g.21858$Sa1.9467@tornado.southeast.rr.com>:
peter koch wrote:
I agree here. Readability matters a lot. And here C++ is a clear
winner, due to its more advanced features such as templates, operator
overloading and RAII,
You are joking, Right?
I think he's serious, but then RAII may or may not be a big issue. I
for one can't tell at this point.
The typical usage paradigm is opening a file:
[a] normal case.
File f = ...; // optional
FileInputStream fs = new FileInputStream(f);
....
fs.close();
[b] with exception handling.
File f = null;
FileInputStream fs = null;
try
{
f = ...;
fs = new FileInputStream(f);
doSomething(fs);
}
catch(Exception ex)
{
log.error("Oops", ex);
}
finally
{
if(fs != null) try { fs.close(); } catch(Exception ex2) {}
}
I've seen prettier but the general idea is clear enough.
In C++:
{
std::ifstream ifs("pathname");
doSomething(ifs);
}
is far simpler; if an exception is thrown std::ifstream::~ifstream
is also guaranteed to be called. However, I have no idea what
{
std::ifstream ifs1("pathname");
std::ifstream ifs2(ifs1);
doSomething(ifs1);
doSomething(ifs2);
}
or
{
std::ifstream ifs1("pathname");
std::ifstream ifs2("path2");
ifs1 = ifs2;
doSomething(ifs1);
doSomething(ifs2);
}
are intended to do; the most logical would be the C++ equivalent
of dup2(). In Java, one runs into object aliasing issues:
FileInputStream fs1 = ...;
FileInputStream fs2 = fs1;
in the foregoing fs1 and fs2 refer to *the same object*. This is either
a bug or a feature.
--
#191, ewill3@earthlink.net
Windows Vista. Because it's time to refresh your hardware. Trust us.