January 2020
Intermediate to advanced
454 pages
11h 25m
English
Example 1 demonstrates how to wrap your library's APIs in a C++ namespace :
// Contents of library.hnamespace library_name{ int my_api() { return 42; } // ...}// Contents of main.cpp#include <iostream>int main(void){ using namespace library_name; std::cout << "The answer is: " << my_api() << '\n'; return 0;}
As shown in the preceding example, the contents of the library are wrapped in a namespace and stored in the header (this example demonstrates a header-only library, which is an extremely useful design approach as the end user doesn't have to compile libraries, install them on his/her system, and then link against them). The library user simply includes the library header file and uses the using namespace library_name statement ...