C和C++中的一些问题
1. C语言是否可以进行重载?测试代码如下:
#include <stdio.h>
#include <stdlib.h>
int f(int a, char b)
{
return a;
}
float f(int a, float b)
{
return b;
}
char f(char a)
{
return a;
}
int main()
{
int a;
float b;
char c;
f(a, c);
f(a,b);
f(c);
return 0;
}
从测试代码可以看出,其中包含了重载的基本情况:参数的个数不同和参数的类型不同。
使用不同的编译器进行测试:
1. CodeBlocks 8.02中,建立C的工程。
||=== test, Debug ===|
\test\main.c|10|error: conflicting types for 'f'|
\test\main.c|5|error: previous definition of 'f' was here|
\test\main.c|15|error: conflicting types for 'f'|
\test\main.c|10|error: previous definition of 'f' was here|
\test\main.c|15|error: conflicting types for 'f'|
\test\main.c|10|error: previous definition of 'f' was here|
\test\main.c||In function `main':|
\test\main.c|24|error: too many arguments to function `f'|
\test\main.c|25|error: too many arguments to function `f'|
||=== Build finished: 8 errors, 0 warnings ===|
编译时使用 mingw32-gcc进行C代码编译。
2. C