The C++ namespace: another simple namespace extension C++ program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Extending the original namespace with overloaded function in C++ programming

To show: How to extend the namespace with overloaded function in C++ programming

 

// the C++ namespace extension

#include <iostream>

using namespace std;

 

struct SampleOne

{};

 

struct SampleTwo

{};

 

// the original namespace

namespace NsOne

{

// original function

void FunctOne(struct SampleOne)

{

cout<<"Processing the struct argument..."<<endl;

}

}

 

using NsOne::FunctOne; // 'using' declaration

// extending the NsOne namespace

namespace NsOne

{

// overloaded function

void FunctOne(SampleTwo&)

{

cout<<"Processing the function argument..."<<endl;

}

}

 

int main(void)

{

SampleOne TestStruct;

// SampleTwo TestClass;

 

FunctOne(TestStruct);

 

// the following function call fails because there are no overloaded version for this one

// FunctOne(TestClass);

 

return 0;

}

 

Output example:

 

Processing the struct argument...

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index