C + + reference, inline function, function overload summary of ambiguity _c++

Source: Internet
Author: User

1. References

1.1 Concepts of references

In the C + + language, you can define references. The references are defined as follows:

Type name & Reference name = A variable name of the same type;

For example: int n;

int &r=n;//r is a reference, you can say that the type of R is int &,r refers to the variable n, or R becomes a reference to N.

A reference to a variable is one thing, the equivalent of an alias for that variable. Note that it is important to initialize a reference when it is defined, otherwise the compilation does not pass, and usually a variable is used to initialize the reference, and after initialization it references the variable and no longer references another variable. You can also use a reference to initialize another reference so that two references refer to the same variable. A reference cannot be initialized with a constant, nor can it be initialized with an expression (unless the return value of the expression is a reference to a variable). In summary, references can only reference variables.

References of type T & are fully compatible with variables of type T and can be assigned to each other.

The sample programs referenced are as follows:

  1. #include <iostream>
  2. using namespace Std;
  3. int main ()
  4. {
  5. int n=4;
  6. int &r=n; R refers to N, since R and N are the same thing.
  7. R = 4; modifying r is modifying n
  8. cout<<r<<endl; Output 4
  9. cout<<n<<endl; Output 4
  10. n = 5; modifying n is modifying R
  11. cout<<r<<endl; Output 5
  12. int &r2 = r; R2 and R refer to the same variable n
  13. cout<<r2<<endl; Output 5
  14. return 0;
  15. }

1.2 Reference as the return value of a function

The return value of a function can be a reference, such as the following program:

  1. #include <iostream>
  2. using namespace Std;
  3. int n = 4;
  4. int & Setvalue ()
  5. {
  6. return n; Returns a reference to n
  7. }
  8. int main ()
  9. {
  10. Setvalue () = 40; The return value is a referenced function call expression that can be used as an lvalue
  11. cout<<n<<endl; Output 40
  12. int &r = Setvalue ();
  13. cout<<r<<endl; Output 40
  14. return 0;
  15. }

1.3 Parameter pass-through value

A "pass value" means that a function's formal parameter is a copy of the argument, and the change of the parameter does not affect the argument during the execution of the function. Take a look at the example:

  1. #include <iostream>
  2. using namespace Std;
  3. void Swap (int a,int b)
  4. {
  5. int temp;
  6. Temp=a;
  7. A=b;
  8. B=temp;
  9. cout<<a<<b<<endl;
  10. }
  11. int main ()
  12. {
  13. int a=4,b=5;
  14. Swap (A, b);
  15. cout<<a<<b<<endl;
  16. return 0;
  17. }

The output of the above program is:

5 4

4 5

In the swap function, the value of the formal parameter, a, or a is indeed interchanged, but A/b in main maintains the original value, that is, the change of the formal parameter does not affect the argument.

1.4 Parameter Passing Reference

In the way of reference, the formal parameter is a reference to the corresponding argument, that is, the formal parameter and the corresponding argument is one thing, then the change of the parameter will change the value of the argument. For example:

  1. #include <iostream>
  2. using namespace Std;
  3. void Swap (int &a,int &b)
  4. {
  5. int tmp;
  6. TMP = A;a = B;B = tmp;
  7. }
  8. int main ()
  9. {
  10. int n1=100,n2=50;
  11. Swap (N1,N2);
  12. cout<<n1<<n2<<endl; Output 50 100
  13. }

After entering the swap function, a refers to the n1,b reference to the change in n2,a,b that affects the value of N1,N2.

2. Inline functions

The use of functions can reduce the volume of code, but also bring the cost of running time, if a function inside the statement is very small, the execution time is very short, then call this function time can not be ignored, in the C + + language, "inline" is a good solution to the problem of function call overhead. A function that adds "inline" is called an inline function, for example:

inline int max (int a,int b);

The effect of adding inline is equivalent to inserting the statement code of the function into the call place at compile time, reducing the access time after allocating space to the variable, but the inline is only suitable for short functions, and for more functions of the statement, if the use of inline, equivalent to pay to increase the volume of several times the cost , but the speed is only increased by one out of 10,000, not worth the candle, sometimes the function looks very simple, but contains the loop to use many times, to consume a lot of time, also not suitable for inline functions. So the intrinsic feature of the inline function is that the code is simple and executes quickly.

3. Overloading of functions

The C + + language does not allow variable names, but allows multiple functions to take the same name, as long as the parameter table is different, which is called the overload of the function. Equivalent to the same thing to accomplish different functions.

So when invoking a function of the same name, how does the compiler call which function? Very simply, the compiler is judged by the number and type of arguments in the function invocation statement. Because the parameter table of an overloaded function is different, it is called whenever a statement of the calling function gives an argument and a parameter table match. For example:

  1. #include <iostream>
  2. using namespace Std;
  3. int max (int a,int b)
  4. {
  5. cout<<max1<<endl;
  6. }
  7. Double Max (double a,double b)
  8. {
  9. cout<<max2<<endl;
  10. }
  11. Double Max (double a,double b,double c)
  12. {
  13. cout<<max3<<endl;
  14. }
  15. int main ()
  16. {
  17. Max (3,4); Call int max (int, int)
  18. Max (2.4,6.0); Call Double Max (double, double)
  19. Max (1.2,3.4,5); Call Double Max (double, double, double)
  20. Max (n/a); Call Double Max (double, double, double)
  21. Max (3,1.5); Ambiguity!!!!!!!
  22. return 0;
  23. }

Ambiguity analysis: In line 21st, the compiler calls int max (int, int), as long as the 1.5 is coerced, the call to double Max (double, double) is also possible, at which point the compiler does not know which function to invoke, and therefore two is ambiguous.

There are two functions with the same name and different number of parameters, but the parameters of the function with many parameters can also be used by default, two semantics may occur, for example:

int sum (int a,int b,int c=0);

int sum (int n,int m);

This is called:

sum (in);

At this point the compiler does not know whether to call the first function as a parameter (1,2,0) or call the second function, resulting in two semantics!

C + + reference, inline function, function overload summary of ambiguity _c++

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.