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.
- class dll_t;
-
- class dll_node_t
- {
- friend class dll_t;
-
- public:
- explicit dll_node_t ();
-
- void data (void *_p_data) {p_data_ = _p_data;}
- void *data () {return p_data_;}
-
- private:
- dll_node_t *prev_;
- dll_node_t *next_;
- void *p_data_;
- };
-
- class channel_t
- {
- public:
- channel_t () : node ()
- {
- node_.data (reinterpret_cast <void *> (this));
- }
-
- private:
- dll_node_t node_;
- };
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.
- template <typename T_NODE> class dll_t;
-
- template <typename T_DATA> class dll_node_t
- {
- friend class dll_t <dll_node_t <T_DATA> >;
-
- public:
- explicit dll_node_t ();
-
- void data (T_DATA *_p_data) {p_data_ = _p_data;}
- T_DATA *data () {return p_data_;}
-
- private:
- dll_node_t *prev_;
- dll_node_t *next_;
- T_DATA *p_data_;
- };
-
- class channel_t
- {
- public:
- channel_t (): node_ ()
- {
- node_.data (this);
- }
-
- private:
- dll_node_t <channel_t> node_;
- };
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