我应该将我的实现放在一个文件中,将原型放在头文件中。但按照我的理解,充满原型的头文件不会很有用。这些是什么东西?其中之一与定义或声明相同吗?
函数原型是声明其参数类型的函数声明。这种区别是历史性的。在 C 中,可以声明没有原型的函数,但在 C++ 中,所有函数声明都是原型(因此在 C++ 中没有区别)。
// In C, this is a declaration but NOT a prototype.
// In C, this function takes an unknown number of parameters.
// In C++, this is a prototype, and the function has no arguments.
void f();
// In C, this is a prototype for a function with no arguments.
// In C++, it is the same thing.
void f(void);
在 C++ 中,除非有函数的声明/原型,否则无法调用函数。那就是头文件的使用。
(在 C 中,您可以在没有声明的情况下调用函数,但这被认为是一个坏主意,并且您只能在really旧代码。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)