CPP NAMESPACES code example

Example 1: c++ custom namespace

//using namespaces
using namespace std;

//creating namespaces
namespace custom{
  class example{
    public:
    	static int method(){
          return 0;
        }
  };
};

//using custom namespaces
using namespace custom;

Example 2: access the namespace members using namespace member function

//Header.h
#include <string>

namespace Test
{
    namespace old_ns
    {
        std::string Func() { return std::string("Hello from old"); }
    }

    inline namespace new_ns
    {
        std::string Func() { return std::string("Hello from new"); }
    }
}

#include "header.h"
#include <string>
#include <iostream>

int main()
{
    using namespace Test;
    using namespace std;

    string s = Func();
    std::cout << s << std::endl; // "Hello from new"
    return 0;
}

Tags:

Cpp Example