In-depth analysis of C ++ class template application code

Source: Internet
Author: User

C ++ has the concept of class inheritance, which means an application that can implement the same functions as inheritance. So what is a C ++ class template? Is it similar to the usage of class functions? First, let's use the following code to introduce in detail how C ++ class templates are applied.

C ++ template code example:

 
 
  1. #include "stdafx.h"  
  2. #include "Stack.h"  
  3. #include < iostream> 
  4. #include < string> 
  5. #include < cstdlib> 
  6. #include < stdexcept> 
  7. int _tmain(int argc, _TCHAR* argv[])  
  8. {  
  9. try  
  10. {  
  11. Stack< int> intStack;  
  12. Stack< std::string> stringStack;  
  13. intStack.push(7);  
  14. std::cout< < "intStack.top() = >"< < intStack.top()
    < < std::endl;  
  15. stringStack.push("Hello!");  
  16. std::cout< < "stringStack.top() = >"< < stringStack.top()
    < < std::endl;  
  17. stringStack.pop();  
  18. stringStack.pop();  
  19. }  
  20. catch(std::exception const& ex)  
  21. {  
  22. std::cerr< < "Exception: "< < ex.what()< < std::endl;  
  23. return EXIT_FAILURE;  
  24. }  
  25. return 0;  

  • Summary of the usage of the C ++ string segmentation Function
  • How C ++ templates limit Actual Application
  • Detailed description of the implementation of the C ++ reflection mechanism
  • How to use C ++ to obtain the current path
  • Discussion on the Application of C ++ array parameters
In this C ++ class template code, the statement [Stack <int> intStack;] shows the class template instantiation and defines a variable process. Like the function template instantiation, the class template instantiation must also provide the type of required parameters. Variables defined after instantiation can call member functions defined by class templates like common variables. You only need to note that the member functions of the class template will be instantiated only when called. For the intStack above, because only the push () member function is called, only the construction and analysis functions of the push member function are instantiated, except that they will be called by default ).

There are two advantages:

First, it can save space and time.

Second, for parameter types of each class template, the operation required by the template is required. For example, if you use a custom class MyClass as a class template Caculator <T> parameter. Because Caculator class templates require that parameter types support the "+" and "-" operations. However, your MyClass class only needs to use the "+" Operation and does not provide the "-" operation. Thanks to the above rules, your MyClass type can still be used as a Caulator <T> parameter. The premise is that you do not use "-" related member functions.

The above is our introduction to the application of C ++ class templates.

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.