Namespace – What’s the hell?(Part 2)

To continue, firstly we consider the term – transitive. What is transitive? It is illustrated in the following example:

#include
namespace nS1{
	int var1{2}; 		// variable nS1::var1
}
namespace nS2{
	using nS1::var1; 	//variable nS1::var is declared inside namespace nS2;
}
int main(){
	using nS2::var1;	//namespace nS2 can access var1 of nS1
	std::cout<<var1<<std::endl; // print 2 on the monitor
}

 In the above example, var1 is in nS1, it is again declared in nS2, so we can access nS1::var1 via nS2::var1.

Another main point is unnamed namespace.

This one make variables in this region global but only internal linkage, it means it can not be used elsewhere (more exact – static variables). Let’s consider the following example:

#include<iostream>
namespace {
	int var1{2}; 		// global variable var1
	int var2{3};		// global variable var2
}
int main(){
	var1++;	//2++ =3
	var2--;	//3--=2
	std::cout<<var1<<std::endl;	//print 3
	std::cout<<var2<<std::endl;	//print 2
}

The main points here are that: using namespace minimize name conflicts when multi files programming style are used (one way that C++ programming encourages).

Let’s examine more about variable scope (linkage) and duration.

About duration, an variable might exist during a block or a program runs. For example if a variable is declared inside a block, its duration is block duration. They are auto, register or no specific variables. Only static variables exist during program running.

About scope and linkage – there is block, file and multi files scope. To illustrate, I will show these terms in the following example:
file ex1.h

#ifndef EX1_H
#define EX1_H
#include <iostream>
int var1{2}; // global variable, external linkage by default
static int var1_1{3}; // static keyword makes this one internal linkage
const int var1_2{4};	// const keyword makes this one internal linkage
extern int var2{5}; // global variable, external linkage
extern const int var2_1{6}; // const keyword makes this one internal linkage
int mul2(const int &x){return 2*x;} // function in C++ has external linkage
static int add(const int &x, const int &y){return x+y;} // static keyword make this function only internal linkage
int count1(const int &x, const int &y){
	int temp=mul2(x);	// local variable - block scope
	return add(temp,y);
}
#endif

file run1.cc

#include <iostream>
#include "ex1.h"
int main(){
	std::cout<<var2<<std::endl;		// print var2 of ex1.h
	std::cout<<var1<<std::endl;		// print var1 of ex1.h
	//td::cout<<var1_1<<std::endl;	// error cause var1_1 of ex1.h is internal linkage
	std::cout<<mul2(var2)<<std::endl;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s