`
universsky
  • 浏览: 91957 次
文章分类
社区版块
存档分类
最新评论

c++: NAMESPACES

 
阅读更多

Namespaces

Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.

The format of namespaces is:

namespace identifier
{
entities
}

Where identifier is any valid identifier and entities is the set of classes, objects and functions that are included within the namespace. For example:

1
2
3
4
namespace myNamespace
{
  int a, b;
}


In this case, the variables a and b are normal variables declared within a namespace called myNamespace. In order to access these variables from outside the myNamespace namespace we have to use the scope operator ::. For example, to access the previous variables from outside myNamespace we can write:

1
2
myNamespace::a
myNamespace::b 


The functionality of namespaces is especially useful in the case that there is a possibility that a global object or function uses the same identifier as another one, causing redefinition errors. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// namespaces
#include <iostream>
using namespace std;

namespace first
{
  int var = 5;
}

namespace second
{
  double var = 3.1416;
}

int main () {
  cout << first::var << endl;
  cout << second::var << endl;
  return 0;
}
5
3.1416


In this case, there are two global variables with the same name: var. One is defined within the namespace first and the other one in second. No redefinition errors happen thanks to namespaces.

using

The keyword using is used to introduce a name from a namespace into the current declarative region. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// using
#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
  int y = 10;
}

namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}

int main () {
  using first::x;
  using second::y;
  cout << x << endl;
  cout << y << endl;
  cout << first::y << endl;
  cout << second::x << endl;
  return 0;
}
5
2.7183
10
3.1416


Notice how in this code, x (without any name qualifier) refers to first::x whereas y refers to second::y, exactly as our using declarations have specified. We still have access to first::y and second::x using their fully qualified names.

The keyword using can also be used as a directive to introduce an entire namespace:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// using
#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
  int y = 10;
}

namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}

int main () {
  using namespace first;
  cout << x << endl;
  cout << y << endl;
  cout << second::x << endl;
  cout << second::y << endl;
  return 0;
}
5
10
3.1416
2.7183


In this case, since we have declared that we were using namespace first, all direct uses of x and y without name qualifiers were referring to their declarations in namespace first.

using and using namespace have validity only in the same block in which they are stated or in the entire code if they are used directly in the global scope. For example, if we had the intention to first use the objects of one namespace and then those of another one, we could do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// using namespace example
#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
}

namespace second
{
  double x = 3.1416;
}

int main () {
  {
    using namespace first;
    cout << x << endl;
  }
  {
    using namespace second;
    cout << x << endl;
  }
  return 0;
}
5
3.1416


Namespace alias


We can declare alternate names for existing namespaces according to the following format:

namespace new_name = current_name;

Namespace std

All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.
分享到:
评论

相关推荐

    sourceinsight4087

    Fix: C++: Anonymous namespaces were not parsed correctly. Fix: C/C++: designated initializers were not being parsed. Fix: C++: Templated class was not parsed correctly for member functions. For ...

    Programming with C++

    Chapter 20: Namespaces and Preprocessor Directives 493 Chapter 21: Standard Template Library 509 Chapter 22: Sequence Containers–vector, list and deque 532 Chapter 23: Associative Containers–set, ...

    Learn.C++.Programming.Language.Become.A.Complete.C++.Programmer.pdf

    Namespaces Chapter 15. Source Files and Programs Part III: Abstraction Mechanisms Chapter 16. Classes Chapter 17. Construction, Cleanup, Copy, and Move Chapter 18. Overloading Chapter 19. Special ...

    ExceptionalCpp_47EngineeringPuzzlesProgrammingProblemsSol.chm

    Name lookup, namespaces, and the Interface Principle Memory management issues and techniques Traps, pitfalls, and anti-idioms Optimization Try your skills against the C++ masters and come away ...

    C++ Programming: From Problem Analysis to Program Design, 8th Edition

    Namespaces, The Class String, And User-Defined Simple Data Types. Chapter 8. Arrays. Chapter 9. Records (Structs). Chapter 10. Classes And Data Abstraction. Chapter 11. Inheritance And Composition. ...

    C++17 标准 ISOIEC 14882 2017 官方pdf文档

    In addition to the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces, operator overloading, function name overloading, references, free store ...

    超清晰PDF C++

    1.1 INTRODUCTION TO C++ 2 Origins of the C++ Language 2 C++ and Object-Oriented Programming 3 The Character of C++ 3 C++ Terminology 4 A Sample C++ Program 4 1.2 VARIABLES, EXPRESSIONS, AND ASSIGNMENT...

    Premier.Press.C++.Programming.for.the.Absolute.Beginner.2001.chm

    Chapter 7: Building Namespaces Chapter 8: Introducing Inheritance Chapter 9: Using Templates Chapter 10: Using Streams and Files Chapter 11: Errors and Exception Chapter 12: Programming with Windows ...

    C++基础教程完整版

    Namespaces 3. 出错处理 Exception handling 4. 类型转换高级 Advacned Class Type-casting 5. 预处理指令 Preprocessor Directives 7. C++ 标准函数库 C++ Standard Library 1. 文件的输入输出 Input/...

    C++ Basic

    1.1 INTRODUCTION TO C++ 2 Origins of the C++ Language 2 C++ and Object-Oriented Programming 3 The Character of C++ 3 C++ Terminology 4 A Sample C++ Program 4 1.2 VARIABLES, EXPRESSIONS, AND ASSIGNMENT...

    Practical C++ Programming C++编程实践

    Variable Scope and Functions Scope and Storage Class Namespaces Functions Summary of Parameter Types Recursion Structured Programming Basics Real-World Programming Programming Exercises Answers to ...

    C++.Programming.From.Problem.Analysis.to.Program.Design.7th.Edition

    Namespaces, the class string, and User-Defined Simple Data Types. Chapter 8. Arrays. Chapter 9. Records (structs). Chapter 10. Classes and Data Abstraction. Chapter 11. Inheritance and Composition. ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Scoping Namespaces Nested Classes Nonmember, Static Member, and Global Functions Local Variables Static and Global Variables Classes Doing Work in Constructors Default Constructors Explicit ...

    ISO/IEC 14882 2014 Programming Languages C++

    In addition to the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces, operator overloading, function name overloading, references, free store ...

    Exploring C++ 11

    Exploring C++ divides C++ up into bite-sized chunks that will help you learn the language one step at a time. Assuming no familiarity with C++, or any other C-based language, you’ll be taught ...

    More Effective C++中文版

    一些经过验证的用来改善程序效率的方法,包括检验C++... 介绍新的语言特性,包括bool、mutable、explicit、namespaces、成员模板、标准模板库等。如果你的编译器不支持这些特性,本书还介绍了如何不利用它们完成工作。

    Real-Time C++, 2nd Edition.pdf

    are introduced including the syntax of C++, namespaces, the C++ standard library and optimization with compile time constants. This chapter uses our target system with the 8-bit microcontroller.

    Working Draft, Standard for Programming Language C++

    the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces, operator overloading, function name overloading, references, free store management ...

    Professional C++, 4th Edition

    this new fourth edition covers them all, including nested namespaces, structured bindings, string_view, template argument deduction for constructors, parallel algorithms, generalized sum algorithms, ...

    more effective c++

    一些经过验证的用来改善程序效率的方法,包括检验C++..., 介绍新的语言特性,包括bool、mutable、explicit、namespaces、成员模板、标准模板库等。如果你的编译器不支持这些特性,本书还介绍了如何不利用它们完成工作。

Global site tag (gtag.js) - Google Analytics