C++在命名空间中声明类和成员函数
虽然很多程序员都熟悉名字空间的概念,但他们常常都是被动地使用名字空间。也就是说他们使用的是第三方定义的成员(如标准库的类和函数),而不是在名字空间中声明自己的类和函数。本文拟讨论如何在名字空间中声明自己的类和函数,以及如何在程序中使用它们。名字空间是一个范畴,它包含类声明,函数声明,常量声明和模板声明等名字空间成员。例如:
<div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">namespace proj_alpha</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">//</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">下面是名字空间</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;"> <wbr>proj_alpha <wbr></span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">的成员</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">class Spy {};</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">void encrypt (char *msg);</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">const int MAX_SPIES = 8;</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">}</span></div>
在上面的例子中,类Spy在一个单独的文件中实现。通常,你是在一个专门的头文件中声明一个类并在不同的源文件中独立地定义其成员函数。那么如何将名字空间成员类分离成多个源文件呢?
下面是名为 Foo.hpp 的头文件,其中定义了一个名为NS的名字空间,它包含类Foo的声明:
<div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">//Foo.hpp</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">namespace NS</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">class Foo</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">public:</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">void f();</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">void g();</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">};</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">}//close NS</span></div>
另外,在一个单独的源文件Foo.cpp中,首先包含头文件Foo.hpp以便实现类Foo的成员函数f()和g():
<div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">//Foo.cpp</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">#include "Foo.hpp"</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">void NS::Foo::f()</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{ }</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">void NS::Foo::g()</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{ }</span></div>
为了使用名字空间成员,必须使用成员的全路径名,它由名字空间后跟::合成原名组成。因此,类Foo的全路径名是NS::Foo。这样编译器便可以知道NS是一个名字空间名,头文件Foo.hpp必须在引用NS之前被包含。
名字空间是可以扩展的。也就是说可以声明类,而且所声明的类在其它的.cpp文件中是相同的名字空间成员:
<div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">//Bar.hpp</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">namespace NS //</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">扩展</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;"> <wbr>NS</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">class Bar</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">public:</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">void a();</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">void b();</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">};</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">}</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">在</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">Bar.cpp</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">文件中:</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">#include "Bar.hpp"</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">void NS::Bar::a()</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{}</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">void NS::Bar::b()</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{}</span></div>
可以看出,虽然Foo和Bar这两个类在不同的头文件中声明,但它们都是名字空间NS的成员。并且编译器和链接器将这两个类看成是同一名字空间的成员。那么,如何在应用程序中使用这些类呢?
在文件main.cpp中,必须要包含声明类Foo和Bar的头文件并加上相应的名字空间引用声明-using:
<div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">#include "Bar.hpp"</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">#include "Foo.hpp"</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">int main()</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">using NS::Bar; //</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">使用名字空间</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">using NS::Foo; //</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">同上</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">Bar b;</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">Foo f;</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">f.f();</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">//...</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">}</span></div>
using声明由关键字using后跟名字空间成员的全路径。这样就使你在using声明范围内使用成员时不用再加路径。上面的例子中,可以直接使用Foo和Bar,因为在main()的开始使用了using声明。如果没有using声明就必须使用全路径成员名。
<div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">int main()</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">NS::Bar b; //</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">全路径名</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">NS::Foo f; //</span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">同上</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">//?</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">}</span></div>
另外,还有一种引用名字空间成员的方法是使用using指令:
<div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">#include "Bar.hpp"</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">#include "Foo.hpp"</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">int main()</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">{</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">using namespace NS; // using <wbr></span><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">指令</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">Bar b;</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">Foo f; <wbr></span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">f.f();</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">//...</span></div><div style="font-size: 14px; margin: 15.6pt 0cm; list-style: none; line-height: 24px;"><span style="word-wrap: normal; word-break: normal; line-height: 21px; list-style: none; color: black;">}</span></div>
using指令由关键字“using namespace”后跟名字空间名构成。在访问名字空间成员时它是使用最少的一种方法,原因是这种方法将所有名字空间成员注入当前的范围,从而增加了潜在的名字冲突。
转自:http://blog.sina.com.cn/s/blog_d95a05280102w9pd.html
页:
[1]