C++: 关于function的declaration与definition的关系(函数声明和定义的关系)(并附一篇转载文章)

以下材料都来源于C++ primer 5th edition

1(Chap 6):重要概念(有时问题想不明白可以带着这个概念再去思考)

A function is a block of code with a name.//

We execute the code by calling the function.

就是说,function是个name(既然是name,那附带的就有scope这方面的问题),我们按照要求call这个name就能execute some code


2(2.2.2:Variable Declarations and Definitions)

重要概念链:To allow programs to be written in logical parts --> C++ supports "separate compilation"//

To support separate compilation --> we need a way to share code across multiple files --> then C++ set the difference between declarations and definitions.(就是说C++之所以要把declaration和definition这两个看上去差不多的东西特意区分开,就是为了能够programming in logical ways,是吗?!)


两个概念:

A declaration makes a name known to the program. (A file that wants to use a name defined elsewhere includes a declaration for that name.)

更进一步的是(6.1.2),Like other names, the name must be declared “before” we can use it. 重点落在before上,就是说,如果你要使用一个name,你就要在使用之前declare it。(一定要在之前,而不是之后)。


A definition creates the associated entity.


3(2.2.2:Variable Declarations and Definitions

一个重要准则:“Variables must be defined exactly once but can be declared many times”我理解不仅是variable如此,function,class都是如此。

详细地说:"To use a variable in more than one file requires declarations that are separate from the variable's definiton. (About declaration)/ /  To use the same varianle in multiple files, we must define that variable in one -- and only one -- file. Other files that use that variable must declare -- but not define -- variable. (About definiton)"。


就是说,写一个程序,可能会把这个程序分成很多个小文件写,但是在所有的小文件中,某个variable(function,class)的definition只能有一份(不管在哪个小文件中),但是,在每个用到这个variable(function,class)的小文件中,这个名字都要能被识别,因此,每个用到这个variable(function,class)的小文件都要有这个variable(function,class)的declaration;更进一步,这个声明必须出现在使用这个variable(function,class)之前!

总结起来就是2个要求:declaration&definiton的份数要求, 和 declaration出现的位置要求



后面附上一篇转载的文章:Declare vs Define in C and C++
http://www.cnblogs.com/destinydesigner/archive/2011/09/06/2168584.html

这篇文章讲了另2个问题:一是当一个小文件中已经有了definition时,还需不需要declaration;二是关于extern用于declaration of variable的问题,暂时不太懂。暂时了解的是会涉及到multidefinition的问题。


第一个问题:一是当一个小文件中已经有了definition时,还需不需要declaration。

答案是“Once something is defined, that also counts as declaring it”也就是不需要了,definition同时也算作declaration;更进一步“so you can often both declare and define a funtion, class or variable at the same time. But you don't have to.”也就是可以既有声明也有definition,但是这是不必要的。


另外,这篇文章的summary也很好。有个重点句子“Often, the compiler only needs to have a declaration for something in order to compile a file into an object file, expecting that the linker can find the definition from another file. If no source file ever defines a symbol, but it is declared, you will get errors at link time complaining about undefined symbols.” 就是说compiler只需要declaration就可以完成工作,生成一个object文件,但是compiler会理解为还有剩余的工作,要待由linker在另一个文件中找到definition。(如果没有任何一个小文件提供了definition,就会出link阶段的error)。


并且,这篇文章好像还提供了一个这样的观点(持思考态度,可能有我的问题)。就是如果在某处提供了某variable的definition,那么这个variable的storage和附近有definition的object安排在一起。(不知理解的对不对)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值