What is a namespace ?
A namespace is used to uniquely identify one or more names from other similar names of different objects, groups or the namespace in general.
Defining namespaces
Following sample code explains how namespaces can be defined for variables, enums and classes.
#include <iostream>
#include <string>
// defining namespace for a variable
namespace abc {
std::string str {"Hello"};
}
// defining namespace for enum
namespace abc {
enum log_type {
ERROR,
WARNING,
INFORMATION
};
}
// defining namespace for a class
namespace abc {
class T1 {
};
}
int main() {
std::cout << abc::str << std::endl;
abc::log_type logt = abc::log_type::ERROR;
abc::T1 t; // instance of T1 created
return 0;
}
Nested namespaces
It is common to define nested namespaces in commercial projects which usually is based on company, project names etc. For example xyzcorp::exchange::pricing.
Note that same file can contain same namespace definitions without any problem.
namespace abc {
namespace xyz {
}
}
namespace abc {
namespace xyz {
}
}
// FROM C++ 17
namespace abc::xyz {
}
int main()
{
return 0;
}
Using namespace
We can declare using statement for namespaces to avoid entering it for all class usages. Typically it is defined after header inclusions, but same can be declared inside functions or blocks etc.
A namespace is used to uniquely identify one or more names from other similar names of different objects, groups or the namespace in general.
Defining namespaces
Following sample code explains how namespaces can be defined for variables, enums and classes.
#include <iostream>
#include <string>
// defining namespace for a variable
namespace abc {
std::string str {"Hello"};
}
// defining namespace for enum
namespace abc {
enum log_type {
ERROR,
WARNING,
INFORMATION
};
}
// defining namespace for a class
namespace abc {
class T1 {
};
}
int main() {
std::cout << abc::str << std::endl;
abc::log_type logt = abc::log_type::ERROR;
abc::T1 t; // instance of T1 created
return 0;
}
Nested namespaces
It is common to define nested namespaces in commercial projects which usually is based on company, project names etc. For example xyzcorp::exchange::pricing.
Note that same file can contain same namespace definitions without any problem.
namespace abc {
namespace xyz {
}
}
namespace abc {
namespace xyz {
}
}
// FROM C++ 17
namespace abc::xyz {
}
int main()
{
return 0;
}
Using namespace
We can declare using statement for namespaces to avoid entering it for all class usages. Typically it is defined after header inclusions, but same can be declared inside functions or blocks etc.
#include <iostream>
void fun_use_namespace()
{
{
using namespace std;
cout << "using std namespace in a block" << endl;
}
}
{
{
using namespace std;
cout << "using std namespace in a block" << endl;
}
}
int main()
{
using namespace std;
cout << "using std namespace" << endl;
fun_use_namespace();
return 0;
}
{
using namespace std;
cout << "using std namespace" << endl;
fun_use_namespace();
return 0;
}
Namespace alias
Namespace alias is a way to define different, shorter name for a long namespace names.
#include <iostream>
namespace abc {
namespace xyz {
namespace klm {
class T1 {
};
}
}
}
int main()
{
namespace cls = abc::xyz::klm; // NOTE: here cls is alias for abc::xyz::klm
cls::T1 t;
return 0;
}
#include <iostream>
namespace abc {
namespace xyz {
namespace klm {
class T1 {
};
}
}
}
int main()
{
namespace cls = abc::xyz::klm; // NOTE: here cls is alias for abc::xyz::klm
cls::T1 t;
return 0;
}
No comments:
Post a Comment