C ++ class study notes ninth function overloading and function pointers

Study Notes content from: Ditai Software College Tangzuo Lin teacher's video, I appreciate your guidance

Function overloading function pointer met:

When the names of overloaded functions assigned to the function pointer:
1. The selection of overloading rules consistent with the function pointer argument list candidate
function type and function type 2 exactly match candidate function pointers, i.e., the type of the function return value is also to match

such as:

int func(int a)
{
  return a;
}

int func(int a,int b)
{
  return a+b;
}

void func(int a)
{
  std::cout<<a<<std::endl;
}

typedef int(*PFUNC)(int a)

When calling PFUNC p = func; when a match is the first function

Note:
1. Overload function must occur in the same scope
2. The compiler needs to select a function return value type parameter list or function
entry address 3. overloaded functions can not be obtained directly by the function name
right approach is:

printf(%p\n”,int(*)(int)func);
printf(%p\n”,int(*)(int,int)func);
printf(%p\n”,void(*)(int)func);

C ++ and C call each other

1. practical engineering C ++ and C code to call each other is inevitable
2.C ++ compiler to compile a manner compatible with the C language
3.C ++ compiler will prefer to use the C ++ compiler way
4.extern keyword (only in C ++ ) can force the C ++ compiler C compiler mode

extern “C”
{
  //code...
}

For example:
In add.h file:

int add(int a,int b);

In add.c file:

int add(int a,int b)
{
  return a+b;
}

More than two files are the C language, when we want to refer add.h C ++ header files in the file, as in the main.cpp,

#include <stdio.h>

extern “C”
{
  #include “add.h”
}

int main()
{
  int c = add(1,2);
  printf(“c = %d\n”,c);

  return 0;
}

So that it compile, otherwise the compiler pointed out that function is not defined, because the add function is defined in the add.c instead add.cpp

How to ensure a certain C code to C compiler compile it?
A: _cplusplus C ++ compiler macros are in a standard macro, all C ++ compilers have, take advantage of this feature, you can do this:

#ifdef _cplusplus
extern “C”
{
#endif
//C code...

#ifdef _cplusplus
}
#endif

Precautions

1.C ++ compiler can not compile a C function overloading
2. decided to compile the function name is the name of the compiled target
3.C ++ compiler mode function name and parameter list compiled into object name
4.C only way to compile the function name as the target name compile

Published 14 original articles · won praise 0 · Views 101

Guess you like

Origin blog.csdn.net/u012321968/article/details/104450169