• 2009-11-04

    C++program language 阅读笔记(7)

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://ikari.blogbus.com/logs/50217461.html

    C++ Program language 阅读笔记
    第七章 Function

    7.1 Function Declarations
    定义三要点:函数名,返回值(if any),形参类型和个数
    编译器会忽略形参名
    7.1.1 Function Definition
    有且只有定义一次
    有时候也会多定义一些参数,如:
    void search(table *t,char *key,const char*){}    //no use of third argument
    inline的原理(english original):The inline specifier is a hint to the complier that it should attempt to generate code for a call of fac()inline rather  than laying down the code for the function once and then calling through the usual function call mechanism.(chinese)内联关键词对于编译器是一种提示,告知编译器应该为FAC函数的调用产生代码,而不是只展开一次函数通过不寻常的途径调用。如果在inline中使用递归的话,就只有看编译器来决定表达式最后的结果了.inline函数也有他自己的独特位置,可以使用static 变量.
    7.1.2 static Variables
    A local variable is initialized when the thread of execution reaches its definition.只有当编译器获得变量值时局部变量才被初始化,但是全局变量是在第一次时运行就初始化,forever。
    A static varialbe provide a function with "memory" without introducing a global variable.静态变量就是增加一块“内存”,防止全局变量带来的不稳定性.

    7.2 Argument Passing
    注意:call-by-referrence argument can make program hard to read and should most ofen be avoid.Bjarne 自己都说引用会使程序复杂,但是我们为什么还要用引用呢?however,be noticeable more efficient to pass a large object by reference.它的优势原来在传递大型对象时。
    CONST用法:1.表示原来的值不被修改;2.提示是为了提高效率而用引用。
    a iteral,a constant,and an argument that requires convensions can be passed as a const& argument.
    !NON-const reference 是不能convension的。!如:
    float fsqrt(const float&);
    void g(double d){
            float r=fsqrt(2.0f);        //此时的2.0f是一个临时值,为了不改变原来的数
            r=fsqrt(r);           
            r=fsqrt(d);            //此时的fsqrt(d)是临时变量,如果没有const,一三都是ERROR,临时变量会被立刻删除,你会SURPRISE!
    }

    7.2.1 Array Argument
    if an array is used as a function argument,a pointer to its initial element is passed.传递指向数组第一个元素的指针.T[]会转为T*。
    C-STYLE STRING ARE ZERO-TERMINAED,不能传递数组大小,以下两种方法解决:
    1.增加一个参数传递数组大小;
    2.使用const
    处理多维数组的一个办法,使用指针数组,如:
    char* day[]={"mon","tue"......}
    也可以使用vector代替built in,low-level array and pointer.

    7.3 Value return
    没有返回值的居然可以这么写:void fun(){return;}
    注意:返回指向局部变量的指针是危险的!the store is reused after the function returns.
    没有返回值的函数可以返回另一个没有返回值的函数,如:
    void g(int *p);
    void h(int *p){/******************/return g(p)};

    7.4 Overload Function Names
    重载的函数名最好是常用的,也是所有重载函数唯一相同的东西。
    匹配过程:exact->promotion->standard promotion->user-defined conversions->using ellipsis
    下面是参考primer的匹配过程:
    候选函数(函数名相同)->可行函数(个数,类型-可隐式转换)->最佳匹配
    关于多个函数形参:
    如果有且仅有一个函数满足下列条件,则匹配成功:
    The match for each argument is no worse than the match required by any other viable function.
    其每个实参的匹配都不劣于其他可行函数需要的匹配。
    There is at least one argument for which the match is better than the match provided by any other viable function.
    至少有一个实参的匹配优于其他可行函数提供的匹配。
    出现二义性时可通过显式转换处理(不推荐)。

    7.4.1 Overloading and return Type
    return type are not considered in overload resolution.
    这是为了维护call context-independent.

    7.4.2 Overloading and Scopes
    Function declared in differrent non-namespace scopes do not overload.

    7.4.3 Manual Ambiguity Resolution
    1.add a version that resolves ambiguties,如:
    inline void f1(int n){f1(long (n));}
    2.强制转换,如:
    static_cast<type>

    7.4.4 Resolution for Multiple Arguments


    7.5 default Argument
    另一种实现重载的方法:
    inline void print(int value){print(value,10);}
    defalut 参数要放最后
    A default argument cannot be repeated or changed in the same scope

    7.6 Unspecified Number of Arguments
    ...means "and maybe some more argument"
    此处注意va_list macro的处理方法,我上次日志有讲.

    7.7 Pointer to Function
    call it || take its address
    complete function type must match exactly
    use typedef to defien a name for a pointer to function type,如:

    typedef void(*PF)();
    PF arr[]={&action1,&action2,...};
    可以:PF* useaction=arr;useaction[1]()就是做action2了

    这里又讲了一个SHELL SORT,没看懂,继续学习。

    7.8 Macros
    能不使用就不使用
    宏可以带参数,但是不能重载,递归和帮你找女朋友,它也不懂C++的一些语法。
    ERROR in a macro will be reported when the macro is expanded.也只有它展开时才能发现错误。
    注意使用::和()
    ##是macro operator,字符串相加

    7.8.1 Conditional Compilation
    #ifndef

    #endif



    收藏到:Del.icio.us