Course Content
Introduction to C++
Imagine you work as a C++ developer at a startup, and you and a colleague mistakenly created a function with the same name as yours in a shared project.
Your programs are not compatible, the processor doesn't understand what specific function you want to call.
Don't worry. Functions will be covered later in this course!
To avoid name conflicts, you and your colleague need to create your own namespaces. The namespace allows you to create your repository with your names for objects that will not conflict with the same object names from other repositories.
Note
Repository is a storage place.
Consider an example with the same functions:
This example throws an error. This is done on purpose.
main.cpp
In this case, we will get an error, because the compiler does not know which function to call: yours or your colleague's.
But if we wrap the functions in their own namespaces, then:
main.cpp
The compiler understands that we want to call the function someFunction()
from the namespace with the name myNameSpace
, and executes this command without any problems. The code will work, and we will get the "This is my function!"
message.
Section 1.
Chapter 4