当前位置:编程学习 > C/C++ >>

第17章 用于大型程序的工具(12)

17.2.6 重载与命名空间

1. 候选函数与命名空间

有一个或多个类类型形参的函数的名字查找包括定义每个形参类型的命名空间。这个规则还影响怎样确定候选集合,为找候选函数而查找定义形参类(以及定义其基类)的每个命名空间,将那些命名空间中任意与被调用函数名字相同的函数加入候选集合。即使这些函数在调用点不可见,也将之加入候选集合。将那些命名空间中带有匹配名字的函数加入候选集合。

namespace Andersoft 

    namespace Namespace7 
    { 
        class Class1{}; 
        void Method1_Class1(Class1 &c1){} 
    } 
 
    namespace Namespace8 
    { 
        class Class1:public Andersoft::Namespace7::Class1{}; 
    } 

namespace Andersoft
{
 namespace Namespace7
 {
  class Class1{};
  void Method1_Class1(Class1 &c1){}
 }

 namespace Namespace8
 {
  class Class1:public Andersoft::Namespace7::Class1{};
 }
}Andersoft::Namespace8::Class1 &c1=Andersoft::Namespace8::Class1(); 
Method1_Class1(c1); 
 Andersoft::Namespace8::Class1 &c1=Andersoft::Namespace8::Class1();
 Method1_Class1(c1);2. 重载与using声明

namespace Andersoft 

    namespace Namespace6 
    { 
        void Method2_Class1(int &i){} 
        void Method2_Class1(double &i){} 
    } 

namespace Andersoft
{
 namespace Namespace6
 {
  void Method2_Class1(int &i){}
  void Method2_Class1(double &i){}
 }
}using Andersoft::Namespace6::Method2_Class1; 
int i=10; 
Method2_Class1(i); 
 using Andersoft::Namespace6::Method2_Class1;
 int i=10;
 Method2_Class1(i);3. 重载与using指示

using指示将命名空间成员提升到外围作用域。如果命名空间函数与命名空间所在的作用域中声明的函数同名,就将命名空间成员加到重载集合中。

4. 跨越多个using指示的重载

如果存在许多using指示,则来自每个命名空间的名字成为候选集合的组成部分。

//Namespace3.h  
#ifndef NAMESPACE3_H  
#define NAMESPACE3_H  
#include <string>  
 
using std::string; 
 
namespace Andersoft 

    namespace Namespace6 
    { 
        void Method2_Class1(int &i){} 
        void Method2_Class1(double &i){} 
    } 

 
namespace Andersoft 

    namespace Namespace7 
    { 
        void Method2_Class1(double &i, string &j){} 
    } 
    namespace Namespace8 
    { 
        void Method2_Class1(double &i, int &j){} 
    } 

 
#endif 
//Namespace3.h
#ifndef NAMESPACE3_H
#define NAMESPACE3_H
#include <string>

using std::string;

namespace Andersoft
{
 namespace Namespace6
 {
  void Method2_Class1(int &i){}
  void Method2_Class1(double &i){}
 }
}

namespace Andersoft
{
 namespace Namespace7
 {
  void Method2_Class1(double &i, string &j){}
 }
 namespace Namespace8
 {
  void Method2_Class1(double &i, int &j){}
 }
}

#endifint i=10; 
Method2_Class1(i); 
double j=10.0; 
Method2_Class1(j); 
string k="123"; 
Method2_Class1(j,k); 
Method2_Class1(j,i); 
 int i=10;
 Method2_Class1(i);
 double j=10.0;
 Method2_Class1(j);
 string k="123";
 Method2_Class1(j,k);
 Method2_Class1(j,i);17.2.7 命名空间与模板

模板的显式特化必须在定义通过模板的命名空间中声明,否则,该特化将与它所特化的模板不同名。

有两种定义特化的方式:一种是重新打开命名空间并加入特化的定义,可以这样做是因为命名空间的定义是不连续的;或者,可以用与命名空间定义外部定义命名空间成员相同的方式来定义特化:使用命名空间名字限定的模板名定义特化。

为了提供命名空间中所定义模板的自己的特化,必须保证在包含原始模板定义的命名空间中定义特化。

namespace Andersoft 

    namespace Namespace9 
    { 
        template<class T> 
        class TC 
        { 
        public: 
            T i; 
        }; 
    } 

namespace Andersoft
{
 namespace Namespace9
 {
  template<class T>
  class TC
  {
  public:
   T i;
  };
 }
}using namespace Andersoft::Namespace9; 
 
int _tmain(int argc, _TCHAR* argv[]) 

    TC<int> tc1; 
    tc1.i=10; 
    TC<string> tc2; 
    tc2.i="123"; 
 
    return 0; 


摘自 xufei96的专栏
 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,