C++ function

function

Parameter passing

  • C++ has two ways of passing parameters: pass-by-value and pass-by-reference. Pass-by-value is to re-initialize an object according to the incoming parameters, and then modify the object, which will not affect the original value; pass-by-reference is the reference of the incoming variable , modify the reference in the function, you can directly modify the original variable (if it is not modified by the const keyword)
  • If you need to modify the variable without using the form of the return value, you can use the pointer to pass the parameter (modify the object pointed to by the pointer) or the reference to pass the parameter

return value

  • For functions whose return value is void, the function can return;return in the form of , if you do not write this sentence, the compiler will execute to the end of the function and then return
  • The compiler will guarantee that a function will only return through one path in the end (that is, if multiple return paths are written, only one of them will be executed)

Some Notes About Function Return Values

  • Do not return references or pointers to local objects. After the
    function is completed, the storage space occupied by it is also released, so when the function terminates, it means that the returned reference or pointer points to a memory area that is no longer valid. Even if the content of this invalid memory area is still the value we need for the time being, this operation is very dangerous.
  • The following code is very dangerous

    string &refAdd(string a, string b)
    {
        // 函数返回时,c所对应的内存空间就会被释放
        string c = a + b;
        return c;
    }
    
    string *refAppend(string s)
    {
        string ret = s.append("2333");
        return &ret;
    }
    
    void test()
    {
        string s = "test";
        string *ret = refAppend( "test" );
        // 下面2句话均会报出异常,但是运行没有问题
        // cout << *ret << endl;
        // cout << refAdd( s, s ) << endl;
    }
    
  • A function cannot return an array (an array cannot be copied), but a function can return a pointer or reference to an array, that is, the pointer or reference points to this reference. When returning an array reference, the array must be defined outside the function, otherwise after the function ends The storage space will be freed

  • Returns an array pointer: https://blog.csdn.net/jxhaha/article/details/70834041
  • Returns an array reference: https://segmentfault.com/q/1010000000580417

    int (*arr())[10]
    {
        int(*ret)[10] = (int(*)[10])new int[10];
        for (int i = 0; i < 10; i++)
            (*ret)[i] = i;
        return ret;
    }
    
    string odd[3] = { "1", "3", "5" };
    string even[3] = { "2", "4", "6" };
    decltype(odd) &sarr(int i)
    {
        return i % 2 == 0 ? even : odd;
    }
    
    // 类型别名
    typedef int arrT[10];
    arrT* arr2()
    {
        return arr();
    }
    
    void test()
    {
        int(*a)[10] = arr( );
        for (int i = 0; i < 10; i++)
            cout << (*a)[i] << " ";
        cout << endl;
    
        arrT* a2 = arr2();
        for (int i = 0; i < 10; i++)
            cout << (*a2)[i] << " ";
        cout << endl;
    
        auto ret = sarr(0);
        for (int i = 0; i < 3; i++)
            cout <<ret[i] << " ";
        cout << endl;
    }
    

Some tips on debugging

  • In the debug state, the assert function can be used to judge and ensure that the running state of the program is normal. The assert function will not work after the macro defines NDEBUG
  • In addition to NDEBUG, the preprocessor also defines __FILE__, __LINE__, __TIME__, __DATE__for easy debugging

  • code

    void test()
    {
        // assert(1 > 2); // 在debug模式下,这句话会使得程序报错,但是在release模式下,这句话不会起作用
        cout << __FILE__ << endl; //当前文件名
        cout << __LINE__ << endl; // 当前行数
        cout << __TIME__ << endl; // 当前时间
        cout << __DATE__ << endl; // 当前日期
    }
    

Use of function pointers (arrays)

  • A function pointer is a pointer to a function, some reference links: https://blog.csdn.net/u012526003/article/details/79747302

  • code

    void test()
    {
        vector<int(*)(int, int)> nums(3); // *与括号不可以省略
        nums[0] = myAdd;
        nums[1] = myMminus;
        nums[2] = &myMminus; //与上面的含义相同
        int a = 2, b = 3;
        cout << (nums[0])(a, b) << endl;
        cout << (nums[1])(a, b) << endl;
        cout << (nums[2])(a, b) << endl;
    
        // 函数指针数组与函数指针的定义
        int (*arr[5])(int, int);
        int(*p)(int, int);
    }
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325486003&siteId=291194637