Object-oriented topics C and C + + (5)-use templates wisely to avoid code redundancy

Source: Internet
Author: User

List of articles in this column

First, what is object-oriented

Second, C language can also achieve object-oriented

Third, the non-elegant features in C + +

Iv. solve the package and avoid the interface

V. Rational use of templates to avoid code redundancy

VI, C + + can also reflect

Vii. single-Case pattern solving the construction order puzzle of static member objects and global objects

Viii. more advanced preprocessor PHP

V. Rational use of templates to avoid code redundancy

Let's talk about how to solve the problem of templates that are not easy to encapsulate.

We provide such a way of thinking, for the list type of common types, we try to take the form of forced type conversion, as far as possible to avoid the misuse of templates.

Similarly, we should avoid direct storage of structs, and use Java-like pointers as a way to pass objects.

Let's start by writing a single-type list

#ifndef LIST_C_H#define LIST_C_Hclass list_c_private;struct list_c_node;class list_c {public:    list_c();    ~list_c();    void insert(void*);    int size();    voidget(int);protected:    list_c_private* priv;};#endif // LIST_C_H

Here we use the packaging method described earlier, reducing the degree of coupling between classes

#include "list_c.h"classlist_c_private{ Public:intSize list_c_node* head;};structlist_c_node{void* DATA;    List_c_node* Next; List_c_node () {data = next =nullptr; }};list_c::list_c () {priv =NewList_c_private (); Priv->head =NewList_c_node ();} List_c::~list_c () {DeletePriv;}voidList_c::insert (void* data) {list_c_node* p; for(p = priv->head; P->next! =nullptr; p = p->next) {} P->next =NewList_c_node (); P->next->data = data;}intList_c::size () {returnPriv->size;}void* List_c::get (intK) {intT List_c_node* p; for(p = priv->head->next, t =0; P! =nullptr&& T! = k; p = p->next, ++t) {}returnP->data;}

This is a simple linked list, just used as an example to write the two methods of inserting and fetching.

For universal support, we write a template that casts the type:

#ifndef LIST#define LIST#include "List_c.h"Template<TypeName T>ClassList{ Public:List() {CList= NewList_c (); }    ~List() {delete clist;}voidInsert (TData) {CList -Insert (void*)Data); } int size () {returnCList -Size (); } T get (int k) {return(T) CList -Get (k); }protected: List_c*CList;};#endif //LIST

In this way, the benefits are, first of all, the template can be encapsulated operations, and secondly, in the encapsulation class, dynamic adjustment of internal instances.
For an incoming type, you can determine whether it is suitable for the current template, and if not, you can make a dynamic error in it.

Finally, the use of the template:

#include <iostream>#include "list"usingnamespacestd;int main(){    list<long> testlist;    testlist.insert(10);    testlist.insert(20);    long k = testlist.get(1);    printf("%d\n", k);    return0;}

Object-oriented topics C and C + + (5)-use templates wisely to avoid code redundancy

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.