C language uses macro to realize static polymorphism

Posted by n9ne on Fri, 10 Jan 2020 19:46:05 +0100

Overview

Polymorphism is an important feature of object-oriented programming.

In C + + language, polymorphism means that functions with different functions can use the same function name, and can call functions with different contents with one function name.

There are two kinds of polymorphism in C + +:

1. Static polymorphism (compile time polymorphism): when a program is compiled, the system can decide which function to call, so it is called compile time polymorphism

2. Dynamic polymorphism: the object that the operation pointer points to is determined dynamically during the program running, also known as runtime polymorphism

C + + polymorphism can be realized by virtual function, abstract class, overlay and template

C language is a process oriented language, but it can also realize polymorphism. It can realize compile time polymorphism through macro and dynamic polymorphism by function pointer

C language uses macro to realize compile time polymorphism

For example, the following example is a part of implementing a bidirectional linked list with a macro

#include <stdio.h>
#include <stdlib.h>

#define INIT_LIST_TYPE(type)                    \
    typedef struct list_element_##type {        \
        struct list_element_##type* next, *pre; \
        void* val;                              \
    } list_element_##type;                      \
    typedef struct list_head_##type {           \
        list_element_##type* elem, *last;       \
        int length;                             \
    } list_head_##type;

#define list_element(type) \
    list_element_##type

#define list_head(type) \
    list_head_##type

#define LIST_FUNC_DECLARE(type)   \                                                          
    list_head_##type* init_list_##type();
                                                   
#define LIST_FUNC_INIT(type)   \
    _init_list(type)           
#define init_list(type) init_list_##type()

#define _init_list(type)                                                              \
    list_head_##type* init_list_##type()                                              \
    {                                                                                 \
        list_head_##type* h = (list_head_##type*)calloc(1, sizeof(list_head_##type)); \
        h->last = h->elem = NULL;                                                     \
        h->length = 0;                                                                \
        return h;                                                                     \
    }

#endif

call

INIT_LIST_TYPE(int);
LIST_FUNC_INIT(int);

INIT_LIST_TYPE(float);
LIST_FUNC_INIT(float);


#define context_bam_elem list_element(int)
#define list_head_t list_head(int)

#define list_element_f_t list_element(float)
#define list_head_f_t list_head(float)


int main()
{
    list_head_t* h = init_list(int);
    list_head_f_t* h1 = init_list(float);
    
    return 0;
}

 

Topics: C Programming