Coming from a good foundation in java to c++, I'm really confused on why when we use the cmath library we don't have to call it like: classname.function like we do in java (ex: Math.sqrt()). I saw that we can just call sqrt(). How does it know which class this method is coming from. I also wasn't able to see wether this function is static or not or any visibility type on cppreference.com so it did not help to give me a clue either.
-
2unlike java, in c++ not everything needs an object. it is a multi-paradigm language – NathanOliver May 15 '22 at 02:08
-
7A good foundation in Java is a horrible foundation for C++. If you want to learn C++ Forget everything you know about Java. – Pete Becker May 15 '22 at 02:08
-
1"How does it know which class this method is coming from." It doesn't come from a class, and it isn't a method. (Even the things in C++ that you *would* call "methods" as a Java programmer, aren't called methods in normal C++ terminology; we call them *member functions* instead.) In fact, Java is really the odd one out when it comes to this heavily class-oriented design. – Karl Knechtel May 15 '22 at 02:31
-
"I also wasn't able to see whether this function is static or not " `static` can mean multiple different things in pretty much every language that uses `static` as a keyword, and the meanings in one language may have very little to do with the meanings in another language. If your question is "how do I know whether I need an instance of the class to call this?", then you don't need an instance of the class to call any ordinary (i.e. non-member) function in C++ - because there is no class to instantiate. You do, of course, need a complete set of arguments, some of which may be class instances. – Karl Knechtel May 15 '22 at 02:33
-
In Java you can get the same effect with import static, see https://stackoverflow.com/questions/6497165/non-class-functions-in-java Functions, which are not member functions, are called free functions in C++. They can be overloaded according to the types of the parameters. C++ is very compatible to C (even started that way). C does not have classes. Often a class is not needed, e.g. if you only want to access the public interface of other classes. I would say that static functions, which do not access private attributes, have a similar role in Java. – Sebastian May 15 '22 at 03:56
2 Answers
It doesn't come from a class. Not every function in C++ is a class method. In fact, most of them aren't. Functions in C++ (and in many languages; Java is really the oddball here) can be standalone and can simply be called as-is.
Likewise, top-level functions can't be "static" in the Java sense, since they don't belong to a class. static
is a keyword in C++, and inside a class it's similar to Java's definition, but you won't find yourself using it very often in C++. Outside a class, "static" means something totally unrelated.
The headline here is: Forget most of what you know about Java. Low-level control flow, like loops and conditionals, will mostly carry over. But C++ classes are a different beast, memory management in C++ is entirely different, and the way you structure your program is different.
Java paradigms work in Java, but if you follow those paradigms in C++, you'll make everything a (raw) pointer, stick everything inside of a class, and litter new
calls throughout your program. I've seen code that's written like this. I've had professors who were obviously Java coders forced to teach C++, and it shows. Learn C++ as its own language, not as "diet Java". I cannot stress that point enough.

- 62,821
- 6
- 74
- 116
-
Outside a class, top-level C++ functions are called without method-call syntax, and are not called "on an instance of the class" - after all, there is no "the class" to speak of. In a manner of speaking, they are all thus "static" in the general Java sense. – Karl Knechtel May 15 '22 at 02:35
In C++ you may opt to use namespace
to achieve exactly that. For example you may declare function doSomething
within namespace Math
and then you refer to that function as Math::doSomething
.

- 3,105
- 1
- 12
- 20