Interpretation of c ++ pointer-heavy application code

Source: Internet
Author: User

C ++ is an advanced C language version. Of course, it has all the functions of C language. To some extent, it also has many practical application functions. Today, we will introduce pointer operations and specific application methods of C ++ pointer overloading.

Example code of C ++ pointer overloading:

 
 
  1. # Include "stdafx. h"
  2. # Include <iostream>
  3. # Include <string>
  4. Template <typename T>
  5. Inline T const & max (T const & a, T const & B)
  6. {
  7. Return a <B? B:;
  8. }
  9. // Evaluate the iterator of the two pointer values
  10. Template <typename T>
  11. Inline T const & max (T * const & a, T * const & B)
  12. {
  13. Return * a <* B? * B: *;
  14. }
  15. // Evaluate the referers of two strings
  16. Inline char const * const & max (char const * const &,
    Char const * const & B)
  17. {
  18. Return strcmp (a, B) <0? B:;
  19. }
  20. Int _ tmain (int argc, _ TCHAR * argv [])
  21. {
  22. // Compare the maximum values of two int values. The first max template is called.
  23. Int a = 7;
  24. Int B = 42;
  25. Std: cout <"max (a, B) =>" <: max (a, B) <std: endl;
  26. // Compare the maximum values of two strings. The first max template is called.
  27. Std: string s = "hey ";
  28. Std: string t = "you ";
  29. Std: cout <"max (s, t) =>" <: max (s, t) <std: endl;
  30. // Compare the maximum value of the two pointers. The second max template is called.
  31. Int * p1 = &;
  32. Int * p2 = & B;
  33. Std: cout <"max (p1, p2) =>" <: max (p1, p2) <std: endl;
  34. // Compare the maximum values of two c strings. The third non-template max function is called.
  35. Char const * s1 = "David ";
  36. Char const * s2 = "Nico ";
  37. Std: cout <"max (s1, s2) =>" <: max (s1, s2) <std: endl;
  38. Return 0;
  39. }

  • Analysis on Application Methods of C ++ object Replication
  • Analysis of basic implementation methods of C ++ Using Interfaces
  • Analysis of the similarities and differences between the left and right values of C ++
  • C ++ template parameters
  • C ++ template function reloads different comments
Note the following when reloading the above C ++ pointer code:

1): max (a, B) and max (s, t) call the same max template function. Because they satisfy the definition of the first template function, they only have different types.

2): max (p1, p2) calls are somewhat mysterious.

3): max (s1, s2) calls the third non-template max function. The second template function is not used to generate new instances.

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.