zoukankan      html  css  js  c++  java
  • C++ Primer 读书笔记 第七章

    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[] = {1, 2, 3, 4, 5, 6};
        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[] = {1,2,3};
        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)) (int *, int);
    
    ff(int) is a function returns int (*)(int *, int);
    
    //use typedef
    typedef int (*PF)(int *, int);
    PF ff(int); //ff returns a pointer to function

     15. Two overloaded functions.

    lookup(Account&)
    lookup(const Account&)
    //class member functions
    Screen& display(ostream &os) const Screen& display(ostream &os) const
  • 相关阅读:
    Java练习 标准输入,输出,以及switch判断
    Java练习 标准输入,输出,以及if else判断
    Java 语句和流程控制
    Java的运算符,Java的表达式
    理解 Linux 的硬链接与软链接(转)
    第一范式、第二范式、第三范式详解(转自知乎)
    TCP/IP协议图解
    POSIX条件变量
    自旋锁与读写锁
    POSIX信号量与互斥锁实现生产者消费者模型
  • 原文地址:https://www.cnblogs.com/null00/p/3098752.html
Copyright © 2011-2022 走看看