Obtain the address of the overload function.

Source: Internet
Author: User

In fact, as long as the compiler determines what value you want to get, it will naturally help you get it. We can't specify the parameter when getting the function pointer. How can we tell the compiler that we need it?

 

In the compiler's opinion, the overloaded functions are basically different functions, of course there will be different function addresses; the addresses we get in the conventional method are only the first overloaded addresses.

The following is a reference to Delphi to explain how to obtain the overloaded function pointer. In fact, C ++ has the same principle.

We can also use this method in C ++. First, define a pointer with the specified parameter type, and then get the function address using the get address and function name. In this way, we use an indirect method to tell the compiler that we need a function with specific parameters, and it can help us get it.

How to obtain overload functions in C ++

VoidFunc (){};VoidFunc (Const Char*,Int){};//Define a pointer to the function type to be obtainedVoid(* Funcpointer )(Const Char*,Int);//Obtain the address of the overload function.Funcpointer = func;

 

In the compiler's opinion, the overloaded functions are basically different functions, of course there will be different function addresses; the addresses we get in the conventional method are only the first overloaded addresses.


CodeFile:

UnitUnit1;

Interface

UsesWindows, messages, sysutils, variants, classes, graphics, controls, forms,Dialogs;

TypeTform1 =Class(Tform) ProcedureFormcreate (Sender: tobject ); End;

VaRForm1: tform1;

Implementation

{$ R *. DFM}

{Example of two overloaded functions first}
FunctionMyfun (s:String):String;Overload;
BeginResult: = s;
End;

FunctionMyfun (I: integer ):String;Overload;
BeginResult: = inttostr (I );
End;

{Obtain the addresses of the overloaded functions respectively}
Procedure Tform1.formcreate (Sender: tobject );
Type {Two method types need to be defined first, and the parameters correspond to the above overload functions respectively} TF1 =Function(S:String):String; TF2 =Function(I: integer ):String;
VaR F1: TF1;{Corresponding to reload 1} F2: TF2;{Reload 2} S1, S2:String;
Begin F1: = myfun;{Let F1 point to reload 1} F2: = myfun;{Point f2 to heavy load 2} {Test function} S1: = F1 ('Abc'); S2: = F2 (123); Showmessagefmt ('% S, % s', [S1, S2]);{Abc, 123}

 
 

{The first two values are the addresses of the two overloaded functions, and the third value is obtained based on the function name, which is the same as the address of the first overload}Showmessagefmt ('% P, % P, % P', [@ F1, @ F2, @ myfun]);

 
 

{Another method}Showmessagefmt ('% P, % P, % P', [ADDR (F1), ADDR (F2), ADDR (myfun)]); {If it is a method in the published area, you can also use methodaddress to obtain the address} End;End.

 
 
 
 

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.