C + + Primer Review notes (seventh chapter)

Source: Internet
Author: User

Limitations of copy arguments (value passing)

Scenarios that are not suitable for copying arguments include:

    • When you need to modify the value of an argument in a function.
    • When a large object needs to be passed as an argument. For real-world applications, the time and storage costs of replicating objects are often too large.
    • When there is no way to implement object replication.
Passing Arrays by reference

Typically, an array is passed as a non-reference type, at which point the array is converted to a pointer to the first element, and the size of the array in the parameter does not make sense. However, if the parameter is a reference to an array, the compiler does not convert the array argument to a pointer, but instead passes the array reference itself. At this point the array size is checked by the compiler.
Cases:

voidPrintvalues (int(&arr) [Ten]){}intMain () {intI0);intj[2] = {0,1};intk[Ten] = {1,2,3,4,5,6,7,8,9,0}; Printvalues (&i);//errorPrintvalues (j);//errorPrintvalues (k);//ok    return 0;}
When a pointer is needed when passing an array, when is a reference required?
    • You should use a pointer parameter when the function needs to work with an array and the body of the function does not depend on the length of the array, in other cases the reference parameter should be used.
    • The advantage of a pointer parameter is that it can be explicitly expressed that the function is manipulating pointers to array elements, not the array itself, and can use any length of real parameter groups to invoke the function, the disadvantage is that the function body can not depend on the length of the array, otherwise it will easily cause the array memory to cross-border access, resulting in incorrect results or causing the program to crash.
    • The advantage of referencing parameters is that the length of the dependent array in the body of the function is safe, with the disadvantage of restricting the set of real arguments that can be passed, and invoking the function only with a real parameter group of length matching.
Three steps to determine the overloaded function

Consider the following four functions and a call:

void f();void f(int);void f(intint);void f(doubledouble3.14);
f(5.6);     //It calls f(double, double)
  1. To determine the candidate function:
    That is to determine all overloaded function sets, the function in the collection is a candidate function, in this case, four F functions.
  2. Select a viable function:
    that is, from all candidate functions, select one or more functions that can be invoked with the arguments in this function call. It must meet two conditions:

    1. The number of function parameters is the same as the argument that is invoked.
    2. each argument type matches the corresponding parameter type, or it can be converted to a corresponding parameter by an implicit type.

    The possible functions in this example are:

     void  F (int ); void  F (double ,  Double  = 3.14 );  
  3. Looks for the best match (if any)
    that is, the one that matches the corresponding shape participation argument in the feasible function.
    If there are only one function that meets the following criteria, the match succeeds:

    1. The match for each argument of the function is not inferior to the matching required by other viable functions.
    2. The function has an argument match that is better than the match provided by other viable functions.
      Potential content: The matching bad and exact match of the type conversion is required.
      such as f (5.6); , call
    void  F (int );  

    means that there is an argument that requires an implicit type conversion
    such as calling

    void  F (double , double  = 3.14 );  
    The

    does not require an implicit type conversion for each argument's match.
    Therefore the function:

    1. The match for each argument is not inferior to void f (int);. The match for the first argument of the
    2. is an exact match, better than the first argument of void f (int).

    So the best match is

void f(doubledouble3.14);

* Note: If the function f(42,1.2); is called, its feasible function is:

void f(intint);                       //(1)void f(doubledouble3.14);          //(2)

for (1), the match of its second argument is inferior to (2), and for (2), the match of its first argument is inferior to (1). Therefore, it has the ambiguity, the call error.

again * Note: type promotion is better than standard conversion
Consider

void func(int);void func(short);func(‘a‘);char->int//类型提升char->short//类型转换

So the actual call isvoid func(int);

Reference to make formal parameters

When invoking a function that references a parameter, passing an rvalue or passing an object that requires a type of conversion is not allowed. For example:

int incr(int &val){ return ++val;}int main(){ short v1 = 0; const int v2 = 42; int v3 = incr(v1); // error: v1 is not an int v3 = incr(v2); // error: v2 is const v3 = incr(0); // error: literals are not lvalues v3 = incr(v1 + v2); // error: addition doesn‘t yield an lvalue int v4 = incr(v3); // ok: v3 is a non const object type int}
const do formal parameter

To eat (Dan) Surprise (Teng) is that although the function's formal parameter is const, the compiler treats the definition of Func as its formal parameter declared as a normal int type:

void func(constint i){    cout << i << endl;}void func(int i){    cout << i << endl;}error C2084: 函数“void func(constint)”已有主体

This usage is intended to support compatibility with the C language, since functions with const or non-const parameters are no different in C.

Miscellanies
int *&v1;
    • This should be understood from right to left, V1 is a reference, and a int* type binding.

    • You can specify a default argument only once for one parameter.

    • Each version of the overloaded function should be declared in the same scope.

    • function pointers can only be initialized or assigned by a function or function pointer of the same type or by a 0-value constant.

    • A direct reference to a function name is equivalent to applying a fetch-address operator on a function name.

    • Pointers to overloaded functions must exactly match one version of the overloaded function.

    • A function in C + + that takes a multidimensional array as a parameter must indicate a dimension other than the first dimension, for example:
 printValues(int (*matrix)[10int rowSize);

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer Review notes (seventh chapter)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.