C language uses void type pointer to implement object-oriented class concepts and abstractions ., Void object-oriented

Source: Internet
Author: User

C language uses void type pointer to implement object-oriented class concepts and abstractions ., Void object-oriented

When C ++ is not used, many new users may think that the C language lacks object-oriented and abstract. In fact, the C language can indirectly implement object-oriented and abstract through a combination.

But the implementation of polymorphism and inheritance is a little troublesome, but it can still be implemented.

Core:

Void pointer can be used to point to any type of pointer.

1 // Basic code
2 void * p;
3 p = (void *) "HelloWorld";
4
5 char * str;
6 str = (char *) p;
7
8 printf ("% s", str); // Output HellWord
 

Through this we can achieve abstraction, so that data structures or functions are no longer highly coupled with specific types.

Thus, like C ++ templates, any type of element is handled.

 

Object-oriented class concept:
The class itself will have a set of properties and a set of public or private method functions. The outside world can instantiate one, thereby creating an object of the class.

In C language, you can use the struct keyword to define a conceptual structure similar to a class.

 

We will now implement a set of abstract object-oriented list containers (List) that can load any object pointer:
#include <stdio.h>
#include <stdlib.h>


#define SIEZ_NAME 200
#define Class struct

//Doubly linked list
typedef Class Struct_List_Node {
    void * item;
    struct Struct_List_Node * next;
    struct Struct_List_Node * previous;
} WList_Node;

typedef Class Struct_WList {
    // class properties
    WList_Node * head;
    WList_Node * end;
    int length;

    // Publish method functions
    void (* push) (Class Struct_WList *, void *);
    void (* destroy) (Class Struct_WList *);
    void * (* pop) (Class Struct_WList *);
    void * (* shift) (Class Struct_WList *);

} WList;


void WList_push (WList * self, void * item) {
    WList_Node * new_node = (WList_Node *) malloc (sizeof (WList_Node));
    new_node-> item = item;
    new_node-> next = NULL;
    new_node-> previous = NULL;
    printf ("Push% p \ n", new_node);
    self-> length ++;
    if (self-> head == NULL) {
        self-> head = self-> end = new_node;
    } else {
        new_node-> previous = self-> end;
        self-> end = self-> end-> next = new_node;
    }
    
}

void * WList_pop (WList * self) {
    if (self-> length <= 0) return NULL;
    WList_Node * pop_node;
    self-> length--;
    pop_node = self-> end;
    pop_node-> previous-> next = NULL;
    void * return_p = pop_node-> item;
    free (pop_node);
    return return_p;

}

void * WList_shift (WList * self) {
    if (self-> length <= 0) return NULL;
    WList_Node * pop_node;
    self-> length--;
    pop_node = self-> head;
    self-> head = self-> head-> next;
    self-> head-> previous = NULL;
    void * return_p = pop_node-> item;
    free (pop_node);
    return return_p;
}

void WList_destroy (WList * self) {
    WList_Node * destroy_node;
    while (self-> head) {
        destroy_node = self-> head;
        self-> head = self-> head-> next;
        printf ("WList_destroy:% p \ n", destroy_node);
        free (destroy_node);
    }
}

void WList_init (WList * self) {
    self-> length = 0;
    self-> head = self-> end = NULL;
    self-> push = WList_push;
    self-> pop = WList_pop;
    self-> shift = WList_shift;
    self-> destroy = WList_destroy;
}


// Test type
typedef Class struct_book {
    char name [SIEZ_NAME];
    int price;
} Book;

int main () {
    //test
    WList * list = (WList *) malloc (sizeof (WList));

    WList_init (list);

    list-> push (list, "Head!"); // C can omit forced conversion, but it is not recommended
    list-> push (list, (void *) 'S');
    list-> push (list, (void *) 66666);
    list-> push (list, (void *) 2);
    list-> push (list, (void *) (char *) malloc (sizeof (char) * 10));
    list-> push (list, (void *) "wc");
    list-> push (list, (void *) (char *) malloc (sizeof (char) * 10));
    list-> push (list, (void *) (char *) malloc (sizeof (char) * 52));
    list-> push (list, (void *) (char *) malloc (sizeof (char) * 100));
    list-> push (list, (void *) (Book *) malloc (sizeof (Book) * 10));
    list-> push (list, (void *) "HelloWorld !!!!");

    printf ("\ nFrist List length:% d \ n \ n", list-> length);
    printf ("Head String:% s \ n \ n", (char *) list-> shift (list));
    printf ("End String:% s \ n \ n", list-> pop (list));
    printf ("List length:% d \ n", list-> length);

    list-> destroy (list);

    getchar ();
    return 0;
}
 

This way we create a decoupled general list container. init is equivalent to a constructor, destroy is equivalent to a destructor.

 

Observe the code carefully, programming list-> xxx can use all its own public functions, but you need to use the init function during initialization.

Then we pass the first parameter as itself every time, that is, it can be implemented like Python object-oriented (although it is automatically passed) to implement object-oriented classes.

Of course, object-oriented includes not only classes, but also a series of behaviors such as polymorphism, abstraction, interfaces, inheritance, etc. These are slightly troublesome in C language.

 

Thanks for your patience.

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.