c simulating overloaded C + + overloads with function pointers

Source: Internet
Author: User
Tags define function

Why is overloading not supported in C, where a function with the same name is not allowed in the same scope?

We all know that overloading is an object-oriented feature of C + +. C does not exist in the language. The so-called overload simply means that a function name can implement different functions, either input parameters are different or the number of parameters is different, or the return type is different. For example, the function add (), in C + + can be easily implemented int,double and other types of parameters of the addition function, but in C language cannot be implemented. The C language implements the overloaded functionality, or, to be exact, a function similar to overloading, which can be implemented by means of a function pointer.

function pointer definition

Form 1: function type (* pointer variable name) (parameter list);

"Function Type" describes the return type of the function, because "()" has a higher precedence than "*", so the parentheses outside the pointer variable name are necessary, and the subsequent "parameter list" represents the parameter list of the function that the pointer variable points to

Form 2:typedef function Type (* pointer variable name) (parameter list);

Difference: The function of a typedef is to define a new type. You can later define function pointer variables with the new type, the first of which can only be used when defining function pointers.

For example:

1 int func (int x); /* Declare a function */

Int (*f) (int x); /* Declare a function pointer */

F=func; /* Assigns the first address of the Func function to the pointer f */

2 int func (int x); /* Declare a function */

Int (*f) (int x); /* Declare a function pointer */

F FA;

Fa=func; /* Assigns the first address of the Func function to the pointer f */

Function pointer Assignment: The function func has no parentheses and no arguments when assigned, since Func represents the first address of the function, so after the assignment, the pointer F points to the first address of the code of the function func (x).

function pointer invocation:

(*f) ();

f (x);

You can use the function pointer in the same way that the function is called, or you can use the function indicator. However, the latter is automatically converted by the compiler to the former form, which is the form of a function pointer. This is a special place for function pointers when compared to pointers to objects.

Difference pointer function: A function can not only bring back the value of an integer data, a character type value and a value of a real type, but also bring back the data of the pointer type to point to an address cell.

Type identifier * Function name (parameter table) int *f (x, y);

#include <stdio.h>typedefstruct_int_param {intparam1; intparam2;} Int_param;typedefstruct_DOUBLE_PARAM_ {Doubleparam1; Doubleparam2;} Double_param;typedefvoid* (*addfunc) (void*);void* INT_ADD_FUNC (void*WParam) {Int_param* LParam = (int_param*) WParam; intres = lparam->param1 + lparam->param2; return(void*) &Res;}void* DOUBLE_ADD_FUNC (void*WParam) {Double_param* LParam = (double_param*) WParam; Doubleres = lparam->param1 + lparam->param2; return(void*) &Res;}void* Add_func (Addfunc F,void*WParam) {    returnf (wParam);}intMain () {Int_param val1= {Ten, -}; Double_param Val2= {30.5,40.5}; void* Res1 = Add_func (Int_add_func, &val1); intRESULT1 = * ((int*) res1); void* Res2 = Add_func (Double_add_func, &val2); DoubleRESULT2 = * ((Double*) res2); printf ("%d%f", RESULT1,RESULT2); return 0; }

Limitations of this approach: the number of overloaded function parameters that are simulated must be the same as the tangent return value. Because a function pointer is defined, the function pointer definition explicitly returns the value of its parameter count.

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.