in
C
No template (
Template
When the mechanism, we use a normal function. We have to implement an addition function, he can return two parameters, and returns a value with an appropriate type according to the type of parameters, you must handle all the code:
Short Sum (Short A, Short B) {Return A B;}
Int sum (int A, int b) {RETURN A B;}
FLOAT SUM (Float A, Float B) {Return A B;
......
Very trouble, you can express "universal function" with C template functions
Template
T Sum (T A, T B)
{
RETURN A B;
}
Save as Sumtest.h
Now, the C compiler can generate an appropriate function based on the parameter type "field" we call the SUM function, then call it. E.g:
#include
#include "Sumtest.h"
Using namespace std;
Int main (void)
{
FLOAT FA = 1, FB = 3, FS;
FS = SUM (FA, FB);
COUT << "SUM (FLOAT, FLOAT) 1and 3 =" << fs << endl;
}
[root @ localhost html] # g -o sumtest sumtest.cpp
[root @ localhost html] # ./sumtest
SUM (Float, Float) 1 and 3 = 4
Look at the code, the C compiler generates a "Float version" SUM function and calls it. If we give the parameter type, the compiler will report an error. E.g
#include
#include "Sumtest.h"
Using namespace std;
Int main (void)
{
FLOAT FA = 1, FS;
INT IB = 3;
FS = SUM (FA, IB);
COUT << "SUM (FLOAT, INT) 1 and 3 =" << fs << Endl;
Return 0;
}
[root @ localhost html] # g -o sumtest sumtest.cpp
SUMTEST.CPP: IN Function `Int Main ():
SUMTEST.CPP: 10: No matching function for call to `sum (Float &, Int
&) '
Since the function template does not support two different types of parameters, the C compiler will report unable to generate a real function, allowing programmers to have the opportunity to know that the call parameters are invoked.
If you are not using a template function, you can compile it even if the parameter type is not exactly consistent. E.g
#include
Using namespace std;
FLOAT SUM (Float A, Float B)
{
RETURN A B;
}
Int main (void)
{
FLOAT FA = 1, FS;
INT IB = 3;
FS = SUM (FA, IB);
COUT << "SUM (FLOAT, INT) 1 and 3 =" << fs << endl; return 0;
}
[root @ localhost html] # g -o sumtest sumtest.cpp
[root @ localhost html] # ./sumtest1
Sum (Float, Int) 1 and 3 = 4
Because of C , the int type can be automatically converted to a FLOAT type, so this is not an error.
Function template is not a real function, it is just a mold of a specific function of the C compiler. So you cannot open the declaration and definition of the function template in different files, and the normal function can do this.
There is also a problem with the C function is the difference between the function of C. Recently, several code written on the C language was recently appeared on the WWW.CHINAUNIX.NET C / C forum, and the C compiler can be compiled, and it will not work with the C compiler. This is this problem.
Published in: 2003-09-25 20:03 Published topic: Some questions about C and C ? (Challenging!) Ask you: I have the following questions in the UNIX environment: compile with C compiler: If my source program is located in a directory, where is the following file: /web/tool.c /Web/webserver.c Description: Some functions are defined in Tool.c without the corresponding Tool.h header file. WebServer.c calls the function in Tool.c, is it possible to call the function in Tool.c without using the TOOL.C file in WebServer.c? Under my system, it is possible when I change to C compiler compile, always prompts the webserver.c to call the function defined in Tool.c. If this is what is going on, how should I solve this? problem? I haven't progressed in a few days. After a few days, I have to deliver it, and the master will save me! ! ! ! !
Published in: 2003-09-25 19:49 Published topic: What is the problem with compiling the C program with C compiler? I compile the C 塬 program with the C compiler. Why there are many functions that have been defined and the compiler prompt is not defined, and my C program is completely normal with the C compiler. What is the reason? The younger brother is waiting for it, please give points to the intensive
Their problems are essentially the same problem: C and C are different for the process of functions. Compared to the function of the C language, C increases overloaded, inline, constel, 40 new mechanisms. The C function can be overloaded, and C is a function that the C compiler must distinguish between the same name but the parameter is different but the parameter is different but the number of parameters. C integrates parameter information into the compiletable symbol of the final output, such as functions Int fun (int) {...}