C++ Primer 读书笔记 - 第七章

    添加时间:2013-5-26 点击量:

    1. To invoke a function we use the call operator, which is a pair of parentheses. The operands to the call operator are the name of the function and a (possibly empty) comma-separated list of arguments.



    #include <iostream>
    
    using namespace std;

    string::size_type find_char(const string &s, char c)
    {
    string::size_type i = 0;
    while (i != s.size() && s[i] != c)
    ++i;
    return i;
    }

    bool is_sentence(const string &s)
    {
    return (find_char(s, .) == s.size() - 1);
    }

    int add(const int &i)
    {
    return (i + 1);
    }

    int main()
    {
    cout
    << (is_sentence(abc) ? YES : NO) << endl;
    cout
    << add(8) << endl;
    int a[] = {123456};
    return 0;
    }


    2. A function may not return another function or a built-in array type.



    #include <iostream>
    
    using namespace std;

    //It is wrong to return built-in array type
    int[] return_arr()
    {
    int arr[] = {123};
    return arr;
    }
    int main()
    {
    int b[] = return_arr();
    return 0;
    }


    3. The compiler treats



    void fcn(const int i);
    
    void fcn(int i);


        as the same.



    f(int 
    f(
    int const


        also treated as the same.


        Because the parameter is a copy of the passed value.


    4. In C++ it is safer and more natural to use reference parameters. A reference must be initialized using an object of the same type as the reference. A nonconst reference may be bound only to nonconst object of exactly the same type.


    5. There are three common programming techniques to ensure that a function stays within the bounds of its array arguments.


        Place a marker as the end of the array.


        C-style        void fun(int a, int len) //len is the length of the array


        C++ style    void func(int begin, int end)


    6. array as the parameter



    void pirntValues(int//this is commonly used
    
    void printValues(int [])
    void printValues(int [10]) //10 is meaningless

    void printValues(int (&arr)[10]) //parameter is a reference to an array; size of array is fixed

    //for multidimensional array
    void foo(int (matrix)[10]) //10 must be specified


    int matrix[10] // array of 10 pointers


    7. When a function returns a reference type, the return value is not copied. Instead, the object itself is returned.


        Reference returns are lvalues


        It may be surprising to assign to the return of a function, but the return is a reference. As such, it is just a synonym for the element returned.



    #include <iostream>
    
    using namespace std;

    int &larger(int &a, int &b)
    {
    return a > b ? a : b;
    }

    int main()
    {
    int a = 4;
    int b = 5;
    cout
    << larger(a, b) << endl;
    larger(a, b)
    = 10;
    cout
    << a = << a << \tb = << b << endl;
    return 0;
    }

    //output:
    //5
    //a = 4 b = 10


    8. Put inline functions in header files. They should be defined in header files.


    9. Whenever an inline function is added to or changed in a header file, every source file that uses that header must be recompiled.


        So we should be careful when we write Makefile.


    10. Const member function -> The object which invokes this function will not be changed.


    11. Default constructor



    class Sales_item {
    
    public:
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs) const //A member function that is defined inside the class is implicitly treated as an inline function
    {
    return isbn == rhs.isbn; }
    Sales_item(): units_sold(
    0), revenue(0) { } //as isbn is string, it has its own default constructor, it will initialized as

    private:
    std::
    string isbn;
    unsigned units_sold;
    double revenue;
    };


    12. Overloaded Functions -> Same name, different parameters.


        Although we cannot pass an integral value to a enum parameter, we can pass an enum to a parameter of integral type.


    13. typedef bool (comFcn) (const string &, const string &);


    14. Returning a pointer to function.



    int (ff(int)) (intint);
    

    ff(
    intis a function returns int ()(intint);

    //use typedef
    typedef int (PF)(intint);
    PF ff(
    int); //ff returns a pointer to function


    容易发怒的意思就是: 别人做了蠢事, 然后我们代替他们, 表现出笨蛋的样子。—— 蔡康永
    分享到: