Remove forced conversions from templates in C ++

Source: Internet
Author: User

Recommended reading: "avoiding potential errors through templates in C ++" C ++ is a strong type language than C, that is, it is more rigorous in matching data types than C, this helps prevent programmers from making mistakes due to carelessness during programming. Due to historical reasons, keywords such as reinterpret _ cast and static_cast used for forced type conversion are retained in C ++, we should try to minimize the use of forced type conversion in programming. Templates help us achieve this goal. Another advantage of reducing the use of forced type conversion is that the program has more maintainability. The following example shows how to use a template to reduce forced conversions in a program. Figure 1 shows the implementation of the Double-Linked List, DLL, and code snippet using the Double-Linked List.

 
 
  1. class dll_t; 
  2.  
  3. class dll_node_t 
  4.     friend class dll_t; 
  5.  
  6. public: 
  7.     explicit dll_node_t (); 
  8.  
  9.     void data (void *_p_data) {p_data_ = _p_data;} 
  10.     void *data () {return p_data_;} 
  11.  
  12. private: 
  13.     dll_node_t *prev_; 
  14.     dll_node_t *next_; 
  15.     void *p_data_; 
  16. }; 
  17.  
  18. class channel_t 
  19. public: 
  20.     channel_t () : node () 
  21.     { 
  22.         node_.data (reinterpret_cast <void *> (this)); 
  23.     } 
  24.  
  25. private: 
  26.     dll_node_t node_; 
  27. }; 
Figure 1Dll_node_t is a class encapsulation of two-way linked list nodes. In addition to the prev _ and next _ member variables used to save the previous and next node pointers, it also has a p_data _ used to save node data _. Because the specific meaning of the data stored by the node depends entirely on the user of the linked list, the p_data _ type is defined as void * to accommodate any type of data. The data () function located in rows 10th and 11 is used to set and obtain the value of the p_data _ variable respectively. The code in lines 19th to 29 in the figure shows the segments of the channel_t class using the dll_node_t class. In the constructor of the channel_t class, when calling the data () function, you must use the forced type conversion method to save this pointer to the p_data _ variable of the node. It is hard to imagine that when we use the data () function to obtain the value in p_data _, we also need to use the forced conversion method to convert it to the pointer of Type channel_t. This part of the code is not listed in the figure ). Figure 2 shows the modified version using the template. I believe that the reader can easily identify that there is no forced type conversion.
 
 
  1. template <typename T_NODE> class dll_t; 
  2.  
  3. template <typename T_DATA> class dll_node_t 
  4.     friend class dll_t <dll_node_t <T_DATA> >; 
  5.      
  6. public: 
  7.     explicit dll_node_t (); 
  8.  
  9.     void data (T_DATA *_p_data) {p_data_ = _p_data;} 
  10.     T_DATA *data () {return p_data_;} 
  11.      
  12. private: 
  13.     dll_node_t *prev_; 
  14.     dll_node_t *next_; 
  15.     T_DATA *p_data_; 
  16. }; 
  17.  
  18. class channel_t 
  19. public: 
  20.     channel_t (): node_ () 
  21.     { 
  22.         node_.data (this); 
  23.     } 
  24.      
  25. private: 
  26.     dll_node_t <channel_t> node_; 
  27. }; 
Figure 2650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A0422Y1-0.jpg "border =" 0 "alt =" "style =" padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; vertical-align: top; border-style: initial; border-color: initial; border-image: initial; border-width: initial; border-style: initial; max-width: pixel PX; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; "/>

 

This article from "to Jane Li cloud" blog, please be sure to keep this source http://yunli.blog.51cto.com/831344/893597

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.