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

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.
分享到:
评论

相关推荐

    C++:面向对象程序设计

    - **命名空间(Namespaces)**:命名空间用于避免全局名字冲突,提高代码的可读性和可维护性。 **3. C++编程基础和应用** 课程还包括了Visual C++的编程基础,以及如何使用C++构建对话框、菜单、工具条等图形用户...

    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 ...

    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++View C++View C++View

    5. **命名空间(Namespaces)**:命名空间用于避免全局作用域中的名字冲突,提高代码的可读性和可维护性。它是C++中组织代码的重要工具。 6. **内联函数和函数重载(Inline Functions and Function Overloading)**...

    C++程序设计原理与实践

    C++的核心特性包括类(classes)、模板(templates)、异常处理(exception handling)、运行时类型信息(RTTI)以及命名空间(namespaces)。通过这些特性,C++支持面向对象编程(OOP)、泛型编程(generic ...

    Problem Solving with C++ (7th edition)

    - **Namespaces and Templates**: Further discussion of namespaces and templates, including advanced usage scenarios. **Programming Project 13.6 and 13.9**: These projects likely involve using advanced...

    Thinking in C++ 中文版(C++编程思想)

    5. **命名空间(Namespaces)**:命名空间用于避免全局作用域中的名字冲突,使得大型项目中的代码组织更加有序。 6. **内存管理(Memory Management)**:C++提供了指针操作,让程序员可以直接控制内存,但同时也...

    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 ...

    C++/CLI教程

    4. 命名空间(Namespaces):命名空间是C++/CLI中用于组织代码的一种机制,它可以通过限定名称来避免命名冲突。在.NET环境中,命名空间通常用于封装相关的类和函数。 5. 类和指针(Classes and Pointers):C++/CLI...

    C++ Thinking C++编程思想 经典 圣经

    5. **命名空间(Namespaces)**:命名空间用于解决命名冲突的问题,使代码组织更加清晰。书中解释了如何有效地使用命名空间来避免命名冲突。 6. **设计模式(Design Patterns)**:书中结合C++语言特性,讨论了常见...

    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 ...

    Google+C+++编程规范.pdf

    * 命名空间(Namespaces):命名空间是C++中的一种组织代码的机制,可以避免名称冲突。 * 嵌套类(NestedClass):嵌套类可以提高代码的可读性和维护性。 * 非成员函数(Nonmember)、静态成员函数(StaticMember)...

    Overview of the New C++ (C++0x)

    - **内联命名空间 (Inline Namespaces)**:提供了一种机制来控制命名空间内的符号可见性,方便版本管理和向后兼容。 - **常量表达式 (Constant Expressions)**:允许在编译时计算表达式的值,从而在运行时避免不必要...

    Exceptional C++ PDF

    7. **命名空间(Namespaces)**:命名空间是组织代码和避免命名冲突的有效手段。书中介绍了如何正确使用和嵌套命名空间。 8. **C++标准库(Standard Library)**:书中涵盖了C++标准库的关键组件,如iostream用于...

    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, ...

    C++参考手册.zip

    6. **命名空间(Namespaces)**:命名空间用于组织代码,避免命名冲突,提高代码的可读性和可维护性。 7. **智能指针(Smart Pointers)**:C++11引入的智能指针(如unique_ptr、shared_ptr、weak_ptr)可以自动...

Global site tag (gtag.js) - Google Analytics