Example 1

Example 1 demonstrates how to wrap your library's APIs in a C++ namespace :

// Contents of library.h

namespace 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() << ' ';
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 to unwrap the library's APIs. If the user has more than one library with the same API names, this statement can be omitted to remove any ambiguity. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset