A thought of C language realizing alternative "polymorphism"

Source: Internet
Author: User
Tags mul

In the Last post, the function polymorphism is realized by the diversification of the parameters, however, there is a real scene is a function of the number of parameters and the same type.

But the operation process is different, for example, + 、-、 *,/and other basic four, which are two yuan far, and the parameters of the type is basically the same, at this time how to polymorphic it.

I think it's kind of interesting. The purpose of polymorphism is to reduce the repetition of the definition, choosing the most appropriate type to automatically match the corresponding function.

(There is also a parent class object that references a subclass instance, but there is no concept of inheritance in C, so this is no longer explained)

So poised, I think it's possible to use the hidden function name to achieve the desired effect (just a way of thinking).

Examples are as follows:

#include <stdio.h>typedef int func_t (int,int); int add (int a, int b) {return a+b;}; int sub (int a, int b) {return a-A;}; int mul (int a, int b) {return a*b;}; int div (int a, int b) {return a/b;};    void print (int *arr, int len) {int i;    for (i=0;i<len;i++) printf ("%d\t", Arr[i]); Putchar (' \ n ');}    int main (void) {void (*FUNC_P1) (int *,int len);    Int (*func_p2[4]) (int,int) = {Add,sub,mul,div}; func_t *func_p3[4] = {Add,sub,mul,div};    equals with func_p2 int array[]={1,22,3,44,5,66};    FUNC_P1 = print; FUNC_P1 (array,sizeof (array)/sizeof (int)); In order to test func-point printf ("%d\t%d\t%d\t%d\n", Func_p2[0] (2,1), func_p2[1] (2,1), func_p2[2] (2,1), Func_p2[3] (2    , 1));    printf ("%d\t%d\t%d\t%d\n", Func_p3[0] (2,1), func_p3[1] (2,1), func_p3[2] (2,1), func_p3[3] (2,1)); return 0;}

GCC compilation

$ Gcc-o Calc CALC.C

$./calc
1 22 3 44 5 66--use of test function pointers
3 1 2 2--Using the FUNC_P2 pointer function for 2+1 | 2-1 | 2*1 | 2/1
3 1 2 2--Use FUNC_P3 function pointer array implementation 2+1 | 2-1 | 2*1 | 2/1

Summary:
For the alternative polymorphism--the same parameter (number and type) is realized by the idea of hiding function name.

We look forward to your generous comment!

A thought of C language realizing alternative "polymorphism"

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.