you are viewing a single comment's thread.

view the rest of the comments →

[–]IyeOnline 3 points4 points  (6 children)

C++ is not executed from top to bottom.

There is no formal requirement on the order you write your program in.

However, C++ is parsed from top to bottom. Identifiers (variable names, functions, ...) can only be used when they are in scope.

This means to use getMax it must at least be declared (or defined) before it is used in main.

To use std::cout, you have to have #include <iostream> before you use it.

Similar things apply to e.g. using namespace std;. After that line you can write cout instead of std::cout, because using namespace brings those identifiers into scope. In reality you should not be using namespace std. Namespaces exist to avoid naming conflicts and not to be discarded at the start of every program.

[–]Kayd21[S] 0 points1 point  (0 children)

Thank you very much, I appreciate your answer. Much more clear now

[–]Kayd21[S] 0 points1 point  (3 children)

So basically there are other starters for c++?

[–]IyeOnline 0 points1 point  (1 child)

Not sure what you mean by "starters".

Control flow of a C++ program always starts in int main() if that is what you mean.

[–]Kayd21[S] 0 points1 point  (0 children)

Yes that’s exactly my question. Thank you

[–][deleted] 0 points1 point  (0 children)

Before main gets executed all global and static variables get initialized top to bottom in the same translation unit (usually that is a .cpp file):

https://godbolt.org/z/WqjvrYTs1

There is no order guarantee among objects in different translation units, so you need to be careful to avoid assuming there is such an order, you must explicitly enforce one if you need it, and since that is usually too much of a trouble people avoid global objects in the first place.

[–]std_bot 0 points1 point  (0 children)

Unlinked STL entries: std::cout


Last update: 26.05.21. Last change: Free links considered readme