I swear C++ gets a bad rap because most people have only seen a gross mixture of C++98 and C in their codebases
A fully modern C++11 codebase is a beautiful thing
I swear C++ gets a bad rap because most people have only seen a gross mixture of C++98 and C in their codebases
A fully modern C++11 codebase is a beautiful thing
That's not how having a global utility function works in any OO language I can think of.
I'm not sure I understand your issue with doing this. You need to put your global (i.e. public static) functions in classes not because Java is forcing OO practices into everything, but because classes effectively serve as Java's translation units. I think they serve this purpose pretty well in practice.
public unit Util {
public void foo() {
// do things
}
}
which would compile down into a bytecode-containing artifact which we could call a unit file, which the JVM would load at runtime using some sort of unit loader. Callers could import the Util unit and then invoke foo with the statement Util.foo();
But then, I don't see the diffence between the above and the following, public class Util {
public static void foo() {
// do things
}
// If we're really pedantic, we can ban construction of Util instances
private Util() {}
}
which compiles down into a bytecode-containing artifact called a class file, which the JVM loads at runtime using a class loader. Callers can import the Util class and then invoke foo with the statement Util.foo();
What's this downside you speak of? Is there something a different kind of translation unit would do that a class currently doesn't?A class with some static methods is just a namespace. In Java it pretty much turns OO off.
Can you point to an example of such? It would be nice as someone who’s been away from C++ for a long time.