您的位置:首页 > 编程语言 > C语言/C++

深入探索c++对象模型之站在对象模型的尖端

2017-03-19 18:22 211 查看
1.Template

对于member function 的“实体”化,只有在member function被使用的时候,c++standard 才要求它们被“具现”出来。

  1>template的错误报告

所有与类型有关的检验,如果牵涉到template参数,都必须延迟到真正的具现操作发生,才得为之。

nonmember和member template function在具现行为发生之前也一样没有做到完全的类型检验。

2>Template中的名称决议方式

Template之中,对于一个nonmember name 的决议结果是根据这个name的使用是否与“用以具现出该template的参数类型”有关而决定的。如果其使用互不相关,那么就以“scope of the template declaration”来决定name。如果其使用有关联,那么就以“scope of the template instantiation”来决定name。

例如:第一种情况

//scope of the template definition

extern double foo(double);

template<class type>

calss ScopeRules{

public:

   void invariant(){  _member = foo(_val); }

   type type_dependent(){ return foo(_member) ; }

private:

     int  _val;

    type _member;

}

第二种情况:

 //scope of the template instantiation

extern  int  foo(int);

scopeRules<int>  sro;

 sro.invariant();//调用的是double foo(double);

sro.type_dependent();//调用的是  int  foo(int);

2.异常的处理

1>Exception Handing 快速检阅。

C++的exception handing 由三个主要的语汇组件构成:

  (1)一个throw子句。他在程序某处发出一个exception。被丢出来的exception可以是内建类型,也可以是使用者自定义类型。

  (2)一个或多个catch语句。每一个catch子句都有一个exception  handler。它用来表示说,这个子句准备处理某种类型的exception,并且在封闭的大括号区段中提供实际的处理程序。

  (3)一个try区段,它被围绕以一系列的叙述句,这些叙述句可能引发catch子句的作用。

  当一个exception被丢出时,控制权会从函数调用中被释放出来,并寻找一个吻合的catch子句。如果没有吻合者,那么默认的处理例程terminate()会被调用。当控制权被放弃后,堆栈中的每一个函数调用也就被 推离。这个程序称为unwinding the stack。在每一个函数被推离函数之前,函数的local class object 的destructor会被调用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: