【发布时间】:2014-10-24 03:59:48
【问题描述】:
原型应该在哪里声明?例如在 include 语句之后,还是在 main 方法之前?我知道它们都可以编译,但被认为更标准或更可修改?
#include <iostream>
#include <cstdlib>
const unsigned int PI = 3.14;
using namespace std;
int myFunc(int x, int y);
long factorial(int n);
int main()
{
//...
return 0;
}
或
#include <iostream>
#include <cstdlib>
int myFunc(int x, int y);
long factorial(int n);
using namespace std;
int main()
{
//...
return 0;
}
或者根本不应该使用它们并且应该最后声明 main?
如果一种方式更易读或更受欢迎,没有人真正解决过。
【问题讨论】:
-
在您的示例中,它没有任何区别。在实际调用函数之前的某个地方。
-
你的两个程序没有任何区别。无论如何,你不应该使用
using namespace std。 -
我建议将它们放在标题中,并在该翻译单元中进行主要独奏。
标签: c++ readability function-prototypes