C++ Basic Learning Notes - Lesson 4 (Function Overloading, C++ and C++ Mutual Call)

Keywords: C

Links to the original text: http://www.cnblogs.com/riasky/p/3455584.html

This section mainly talks about the main concepts of function overloading and its usage, as well as the criteria of C and C++ calling each other and the specific engineering skills.

function overloading

1. Basic concepts

Function overloading is the definition of different functions with the same function name. Use different function parameters to match the same function name. The basic routines are as follows:

 

#include <stdio.h>
#include <string.h>

int func(int a)
{
	return a;
}

int func(int x, int y)
{
	return x*y;
}

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

int func(char *p)
{
	return strlen(p);
}

int main()
{
	printf ("func(1) = %d\n", func(1));
	printf ("func(1,2) = %d\n", func(1,2));
	printf ("func(1,2,3) = %d\n", func(1,2,3));
	printf ("func(abcdef) = %d\n", func("abcdef"));
}

 

2. Heavy Load Conditions

 

The number of parameters is different.

(2) Different parameter types.

(3) The order of parameters is different.
The teacher in the course said that these three conditions are out of order, that is, as long as one condition is satisfied, we can judge which function to overload, but I think there is a problem.

Routine:

 

#include <stdio.h>
#include <string.h>

int func(const char *p, int b)
{
	return strlen(p); 
}

int func(int b, const char *p)
{
	return b ;
}

int main()
{
	printf ("func(1,ac) = %d\n", func(1,"ac"));
	printf ("func(ac, 1) = %d\n", func("ac", 1));
}

From the above program, it is proved that the condition (3) is that functions with different order of parameters can be overloaded. In C++, functions with similar functions can be overloaded. Compilers can only realize different compilations according to different parameters, thus realizing the recognition and invocation of overloaded functions.

 

3. Conflict between overloading of functions and default parameters of functions

Routine:

 

#include <stdio.h>
#include <string.h>

int func(int a, int b, int c = 0)
{
	return a * b * c; 
}

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

int main()
{
	printf ("func(1,2) = %d\n", func(1,2));
}

At this point, the compiler will admit that the two functions func are overloaded functions, but the compiler will report errors, the error report is: the call to overloaded functions is not clear. This is called program ambiguity. When we use function overloading, we do not use the default parameters of the function. When we use the default parameters of the function, we do not overload the function.

 

Criteria for 4.C++ Compilers to Call Overloaded Functions

 

When we overload functions, we have many functions with the same name. How these functions are compiled into executable programs depends on the compilation criteria of the C++ compiler.

In the first two criteria of successful matching, the preceding small examples can be reflected. The following process is for matching parameters by default type conversion criteria. The process is as follows:

 

#include <stdio.h>
#include <string.h>

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

int func(int b)
{
	return 1;
}

int main()
{
	printf ("func(1,2) = %d\n", func('a',2));
}

Here,'a'is transformed into ASII code to match the parameters of the function.

 

Question: The teacher said that the three criteria for overloading functions are not sequential, but when there is a conflict between the second and third criteria, they are sequential in the C++ compiler environment of VS2008, and the functions will overload the matches first. The routine is as follows:

 

#include <stdio.h>
#include <string.h>

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

int func(char a, int b)
{
	return 1;
}

int main()
{
	printf ("func(1,2) = %d\n", func('a',2));
}

The print result of the above program is 1.

 

 

5. Notes for overloaded functions

(1) Overload functions are essentially different functions independent of each other.

 

(2) The types of overloaded functions are different.

(3) The return value of function can not be used as the basis of overloaded function.

The overload function is determined by the function name and parameter list.

6. Function overload and function pointer

When using overloaded function name to assign function pointer, the overloaded function is defined as overloaded function according to overloaded rule and function pointer parameter list. At the same time, the type of function and function pointer is very strict.
Routine:
#include <stdio.h>
#include <string.h>

int func(int x) // int(int a)
{
    return x;
}

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

int func(const char* s)
{
    return strlen(s);
}

typedef int(*PFUNC)(int a); // int(int a)

int main(int argc, char *argv[])
{
    int c = 0;
    PFUNC p = func;
    
    c = p(1);
    
    printf("c = %d\n", c);
    
    printf("Press enter to continue ...");
    getchar();	
    return 0;
}

Mutual calls between C and C++

1. It's inevitable to incorporate C and C++ code into a real project (recalling the days of learning embedded systems, albeit novice ones). Although C++ compiler can be compatible with C language compilation, C++ compiler will first use C++ to compile and use extern keyword to force C++ compiler to compile code in C mode. C++ and C are compiled in different ways, because languages are different. These ways mainly include some rules and some constraints.

2.C++ (main.cpp) calls functions written in C language (add.c)

The code is as follows:

main.cpp

 

#include <stdio.h>

extern "C"
{
#include "add.h"
}


int main()
{
    printf("1 + 2 = %d\n", add(1, 2));
    
    return 0;
}

add.c

 

 

#include "add.h"

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

Compiling process: 1. G C C add.c-o add.o or gcc-c add.c 2. g++ main.cpp add.o is enough.

 

3.C language (main.c) calls functions written by C++ (add.cpp)
The code is as follows:

main.c

 

#include <stdio.h>
#include "add.h"

int main()
{
    printf("1 + 2 = %d\n", add(1, 2));
    
    return 0;
}

add.cpp

 

 

extern "C"
{

#include "add.h"

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

}

Compiling process: 1. g++-c add.cpp 2. GCC main.c-lstdc++add.o
Question: The process of compiling each other here is to call two different compilers gcc and g++, but the standards and function link libraries are bound to be different. That is to say, although C++ is the development of C+, problems still arise, but this problem is concealed by the compiler? Or is this where C++ is advanced? The difference between the two compilers and the fusion of the two standards?

 

4.C++ and C Mutual Call Consent Solution

_ cplusplus is a standard macro definition built in c + + compiler, which allows c code to be compiled not only by c compiler, but also by c mode in c + + compiler (using conditional compilation). The code is as follows:

 

#include <stdio.h>
#include <string.h>


#ifdef __cplusplus
extern "C" {
#endif

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

int func(const char* s)
{
    return strlen(s);
}

#ifdef __cplusplus
}
#endif

int main(int argc, char *argv[])
{
    printf("Press enter to continue ...");
    getchar();	
    return 0;
}

Note: Between the two braces of extern "C", you can put the definition or declaration of a function, not just the definition of a function.
5. C++ compiler cannot compile multiple overloaded functions in C language

 

Error code example:

 

#ifdef __cplusplus
extern "C"{
#endif

void f()
{
	
}

void f(int i)
{

}
#ifdef __cplusplus
}
#endif

int main()
{
	return 0;
}

 

Reprinted at: https://www.cnblogs.com/riasky/p/3455584.html

Posted by quark76 on Sat, 27 Jul 2019 08:30:21 -0700