This is confusing me a great deal. All code examples being namespaceA
and namespaceB
and the method names being foo()
and bar()
are not helping, either. The way everyone explains it makes it seem as though namespaces are a relic of pre-OOP times where you could not say 'class car give fuel level' but had to go to this from another approach. But when I now want to do a C++ level, what is the point of using namespaces? Not that headers are confusing enough already, namespaces make absolutely no sense to me, in either how they work or why to use them.
Let's say I have a project built around Traffic
, for example. You'd have classes for the Car
s and its components, the Driver
s and Passenger
s and the Road
.
Now, the Road
has Car
s and each Car
has Person
s. What would this look like?
Would you have the namespaces Road
, Car
, Person
? Would the main()
use the namespace Road
to access the stuff in the header? Would the namespace Road
include the namespace Car
, and the Car
include the namespace Person
, and would through this the main()
have access to the methods in Person
? This is the way most guides explain this, but I don't really see the advantage of this over just importing the header file, wouldn't that have the same effect?
Or would you put multiple headers in the same namespace, such as namespace Traffic
with all those classes? Can you nest namespaces?
I know C# and never knew it had namespaces until I looked it up just now, and never needed it, and in Java, Python, and Dart, those also never came up. Since I am trying to teach myself C++, I am kind of stranded here now, asking this question here. So far, I also never used them in C++, but I want to learn it properly.