Evgenii Legotckoi
Oct. 15, 2017, 2:41 a.m.

C++ - Tutorial 010. Namespaces

In addition to functions, classes, and enumerations, C ++ provides namespaces as a mechanism for controlling and controlling the repeated names of functions and classes. For example, if you name some of your functions with names that intersect with the function names in the standard library, then you get a name conflict, that is, a function override from the standard library, which can lead to ambiguity and errors in the program. Thanks to the namespace, you can avoid this problem by simply placing functions in namespace .


  1. namespace My_code
  2. {
  3. class complex { /* ... */ };
  4. complex sqrt(complex);
  5. // ...
  6. int main();
  7. }
  8.  
  9. int My_code::main()
  10. {
  11. complex z {1,2};
  12. auto z2 = sqrt(z);
  13. std::cout << '{' << z2.real() << ',' << z2.imag() << "}\n";
  14. // ...
  15. };
  16.  
  17. int main()
  18. {
  19. return My_code::main();
  20. }

In this case, the My_code space allows you to write a separate implementation of the main() function, which will be called in the main() function, so you need to call the main function, through the namespace My_code .

  1. My_code::main();

Similarly, functions are called from the standard library, for example:

  1. std::max(3, 5);

In order to make life easier and not to write std every time, you can use the using directive.

  1. using namespace std;

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html