简体   繁体   English

C ++中函数和方法的声明顺序是否重要

[英]Does the order of declaring functions and methods in C++ matter

In C when you use a function before declaring it the compiler assumes that is takes no parameters and returns and int. 在C语言中,当您在声明函数之前使用函数时,编译器会假定该函数不带任何参数,然后返回int。
If your function returns another type or takes parameters then the compiler produces an error. 如果函数返回其他类型或采用参数,则编译器将产生错误。
Does the same happen in C++ if I make an object of a class that is declared later on in the code? 如果我创建一个稍后在代码中声明的类的对象,在C ++中会发生同样的事情吗?

In C++ there is no implicit declaration, neither of classes nor of functions. 在C ++中,既没有类也没有函数的隐式声明。 So your question does not apply directly. 因此,您的问题并不直接适用。

If you call a function or create an object of a given type, that function/type has to be declared before the first use. 如果调用函数或创建给定类型的对象,则必须在首次使用之前声明该函数/类型。 Functions do not necessarily have to be defined. 功能不一定必须定义。 and classes only have to be defined when actually instantiating them, or using them as (member) variables. 仅在实际实例化它们或将它们用作(成员)变量时才需要定义类。 When only a pointer to a class is needed a declaration is not needed, as a forward-declaration is sufficient until the memory of the actual object is allocated or functions are called on the object. 当仅需要指向类的指针时,就不需要声明,因为前向声明就足够了,直到分配了实际对象的内存或对该对象调用了函数。

The order does matter. 顺序很重要。 If you reference a function which has not been declared (just the signature and return type, no implementation) then the compiler will throw an error. 如果引用的函数尚未声明(仅签名和返回类型,没有实现),则编译器将引发错误。 The definition of your function can wait until link time. 函数的定义可以等到链接时间。 AFAIK, there is not implicit declaration in C++. AFAIK,C ++中没有隐式声明。

Usually you put the declarations of your functions in header files. 通常,您将函数的声明放在头文件中。 Traditionally, the symbols exported by a translation unit (usually a stand-alone source file eg, hello.cpp ) will be made available through a similarly-named header file (eg, hello.h ). 传统上,翻译单元(通常是独立的源文件,例如hello.cpp )导出的符号将通过名称相似的头文件(例如hello.h )提供。 The implementation then follows in the source file. 然后,实现将在源文件中进行。 Every translation unit can then include header files from other translation units (eg other.h ). 每个翻译单元然后可以包括来自其他翻译单元(例如other.h )的头文件。

Every translation unit gets compiled individually (ie a source file such as hello.cpp ; all #include preprocessing statements are replaced by the actual contents of the files to be included). 每个翻译单元都进行单独编译(即源文件,例如hello.cpp ;所有#include预处理语句都将由要包含的文件的实际内容替换)。 At link time, the implementations of the functions in different translation units get linked together. 在链接时,不同翻译单元中的功能实现会链接在一起。 If this linking step fails, then you can still encounter errors. 如果此链接步骤失败,那么您仍然会遇到错误。

In early c++ version, if you define the function test(int a) , it will return a int type value by default. 在早期的c ++版本中,如果定义函数test(int a) ,则默认情况下它将返回一个int类型值。 But after c++ becoming standard, the function will get an error. 但是在c ++成为标准之后,该函数将出现错误。 You can find the introduction in the book c++ Primer , the function chapter . 您可以在《 c++ Primer 》一书中的“ function chapter找到介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM