Problems that will also occur when parsing C ++ veterans

Source: Internet
Author: User

For users who have just come into contact with C ++, C ++ does have more complex problems than other development languages. When learning C ++, you should pay attention to the problems from easy to difficult and step by step, to become a C ++ veteran, it is not that easy, and it must take a lot of effort.

For example: const int a; and int const B;, although the two have the same meaning, but it is best to use the latter situation, the latter is more readable, because: int const, we can clearly see that const modifies the int, and in the previous method, we do not easily know the specific meaning of the const. From the method below, we can know that const refers to a constant integer. Int * const B refers to a constant pointer B, which points to an integer.

So the content of this pointer can be changed, but its pointer value, that is, the value of B, cannot be changed. The corresponding int const * B, refers to a pointer B pointing to the content of a constant integer, that is to say, the content of B can be changed, but the content in the address pointed to by B cannot be changed through B in the program. Volatile applies to the above principles. Using type T as a type variable has been used as a template parameter convention to indicate that all types can be used for type parameters accepted by functions or classes.

  • How do I call C ++ functions?
  • Dialysis C ++ array type
  • Understanding the basics of C ++ pointers
  • Interpret and analyze the C ++ linked list
  • Introduction to the working principle of the C ++ iterator

Before the type parameter of the C ++ template, it is best to use typename instead of class. During the compilation period, the template is compiled twice. Before instantiation: Check the template Code itself to see if the syntax is correct. During instantiation, check the template code to see if all calls are valid.

When a function template is used and template instantiation is triggered, the compiler needs to view the template definition. In the derivation of the type of the function arguments, if the type does not match, a compilation error occurs. If you want to solve this compilation error, you can solve the problem by converting the type of the passed arguments to the matching type before passing in the arguments. Display the full features of the specified template function cannot be partial, because the function does not support partial features) type.

Example:

 
 
  1. Template< TypenameT>
  2. T & max (T & value1, T & value2)
  3. {
  4. Return value1>Value2? Value1: value2;
  5. }
  6. When max (3, 4.1) is called, a compilation error occurs. The solution is as follows:
  7. 1. max (static_cast< Double >(3) 4.1 );
  8. 2. max< Double >(3 );
  9. 3. Change the template parameter of the function from one to two.

The default template parameters cannot be specified within the template function. A function cannot implement type recursion using a special method, but it can use a function overload method to implement type conversion. Compared with Class 8, classes can use a special method to implement type recursion.

In addition, its function-like functions can also be overloaded using operator. However, one of its problems is that when calling a function imitation, you must add its instantiated parameter type and call its constructor. When calling a function, you can use the parameter type to push back the template parameters of the function. This is not applicable to function imitation. Therefore, in the programming process, we must pay attention to the advantages and disadvantages between these different technologies to see which ones are more suitable for us.

When calling a non-standard function, it is best to distinguish it from calling a standard function, which does not cause ambiguity errors in the application. The method is to add a GLobal IDEntifier before a variable or function.

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.