Q:Im a bit confused. What is the difference between forward declaration and forward reference? Forward declaration is, in my head, when you declare a function that isnt yet implemented, but is this incorrect? Do you have to look at the specified situation for either declaring a case "forward reference" or "forward declaration"?

A1:

From Wikipedia:

Forward Declaration

Declaration of a variable or function which are not defined yet. Their defnition can be seen later on.

Forward Reference

Similar to Forward Declaration but where the variable or function appears first the definition is also in place.

A2:

A forward declaration is the declaration of a method or variable before you implement and use it.  The purpose of forward declarations is to save compilation time.

The forward declaration of a variable causes storage space to be set aside, so you can later set the value of that variable.

The forward declaration of a function is also called a "function prototype," and is a declaration statement that tells the compiler what a function’s return type is, what the name of the function is, and the types its parameters. Compilers in languages such as C/C++ and Pascal store declared symbols (which include functions) in a lookup table and references them as it comes across them in your code. These compilers read your code sequentially, that is, top to bottom, so if you don't forward declare, the compiler discovers a symbol that it can't reference in the lookup table, and it raises an error that it doesn't know how to respond to the function.

The forward declaration is a hint to the compiler that you have defined (filled out the implementation of) the function elsewhere.

For example:

int first(int x); // forward declaration of first

...

int first(int x) {
if (x == 0) return 1;
else return 2;
}

But, you ask, why don't we just have the compiler make two passes on every source file: the first one to index all the symbols inside, and the second to parse the references and look them up? According to Dan Story:

When C was created in 1972, computing resources were much more scarce  and at a high premium -- the memory required to store a complex  program's entire symbolic table at once simply wasn't available in  most systems. Fixed storage was also expensive, and extremely slow, so  ideas like virtual memory or storing parts of the symbolic table on  disk simply wouldn't have allowed compilation in a reasonable  timeframe... When you're dealing with magnetic tape where seek times  were measured in seconds and read throughput was measured in bytes per  second (not kilobytes or megabytes), that was pretty meaningful.

C++, while created almost 17 years later, was defined as a superset  of C, and therefore had to use the same mechanism.

By the time Java rolled around in 1995, average computers had enough  memory that holding a symbolic table, even for a complex project, was  no longer a substantial burden. And Java wasn't designed to be  backwards-compatible with C, so it had no need to adopt a legacy  mechanism. C# was similarly unencumbered.

As a result, their designers chose to shift the burden of  compartmentalizing symbolic declaration back off the programmer and  put it on the computer again, since its cost in proportion to the  total effort of compilation was minimal.

In Java and C#, identifiers are recognized automatically from source files and read directly from dynamic library symbols. In these languages, header files are not needed for the same reason.

A forward reference is the opposite. It refers to the use of an entity before its declaration. For example:

int first(int x) {
if (x == 0) return 1;
return second(x-1); // forward reference to second
} int second(int x) {
if (x == 0) return 0;
return first(x-1);
}

Note that "forward reference" is used sometimes, though less often, as a synonym for "forward declaration."

from:http://stackoverflow.com/questions/696562/forward-reference-vs-forward-declaration

Forward reference vs. forward declaration的更多相关文章

  1. 错误代码: 1247 Reference 'startTime' not supported (forward reference in item list)

    1.错误描述 1 queries executed, 0 success, 1 errors, 0 warnings 查询:SELECT a.createUserId AS typeId, (SELE ...

  2. wpf staticresource 是不允许向前引用(forward reference)的

    不允许向前引用(forward reference)在C/C++中中很常见,即在语法上,未定义变量.类之前,不能使用. 没想到wpf中的wpf staticresource也遵循这种规则.资源字典中, ...

  3. vector3.forward和transform.forward的区别!

    http://blog.163.com/bowen_tong/blog/static/20681717420146654927791/ vector3.forward和transform.forwar ...

  4. Unity3d vector3.forward和transform.forward的区别!

    原文连接: http://blog.csdn.net/kaluluosi111/article/details/17206655 在unity3d中有2个forward,一个是vector3.forw ...

  5. unity------------------------------transform.forward与Vector.forward的区别

    在unity3d中有2个forward,一个是vector3.forward和transform.forward,这两个forward其实完全不一样.他们之间的区别主要体现在在不同坐标系时的反映上. ...

  6. forward reference前向引用,gloal values and local values全局变量和局部变量,recursive function递归函数

    1.全局变量与局部变量练习 1 # -*- coding: UTF-8 -*- 2 def bar(): 3 print('from bar') 4 def foo(): 5 print('from ...

  7. forward reference extends over definition of value

    在scala代码中定义了一个方法,,刚开始直接代码中报错,,后来编译是一直报错,最后只是在sc.stop后边加了一个中括号解决,方法体不能放在main主函数中

  8. transform.forward和vector3.forward

    Vector3.forward的值永远是(0,0,1)(这里的(0,0,1)是世界坐标的(0,0,1)),而transform.forward我们可以理解为其对应物体的z轴方向,是一个向量,而不是一个 ...

  9. Unity3d Transform.forward和Vector3.forward的区别!

    在Unity中有两个forward,一个是Transform.forward一个是Vector3.forward. 对于Vector3来说,它只是缩写.没有其它任何含义. Vector3.forwar ...

随机推荐

  1. VirtualBox安装Ghost XP

    http://jingyan.baidu.com/album/5d368d1e1a88b73f60c05721.html?picindex=1

  2. mongodb基本操作及存储图片显示方案

    先介绍下mongodb的基本操作及使用 第一部:开启安全性验证 如果需要给MongoDB数据库使用安全验证,则需要用--auth开启安全性检查,则只有数据库认证的用户才能执行读写操作,开户安全性检查, ...

  3. ios 实现跳转到评价界面的两种方式

    要想在App内跳转到特定App的详情页或者评论页,首先需要获取到App的id.在 iTunes Connect网站上登陆之后,选择“我的App”,然后点击某个特定的App进入,在App信息的综合信息中 ...

  4. Java-struts2的问题 java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils

    缺commons-lang3-3.1.jar,添加之后就可以了

  5. HDOJ--4869--Turn the pokers【组合数学+高速幂】

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4869 题意:有m张扑克.開始时所有正面朝下.你能够翻n次牌,每次能够翻xi张.翻拍规则就是正面朝下变背面朝 ...

  6. mybatis01

    mybatis是一个java持久层框架,java中操作关系型 数据库用的是jdbc,mybatis是对jdbc的一个封装. jdk1..0_72 eclipse:eclipse-3.7-indigo ...

  7. 精《记叙“tom”4年的软件开发之旅》

    1.引言 本篇文章是记叙tom四年的软件开发从业经历,虽然他资历不长,况且本身也是个菜鸟,但他也曾有过荣誉.迷茫.困惑与选择,在这里他希望通过自己所经历过的事情分享给大家,给那些真正热爱软件开发的同学 ...

  8. Prototype 原型模式

    用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. 原型模式即在运行时动态的通过一个对象的实例来创建这个类的对象,可以理解成通过一个实例克隆出另一个实例. Prototype模式的一些优 ...

  9. USB开发——内核USB驱动+libusb开发方法

    linux内核已经具备usb驱动,所以一些设备使用可以免驱,libusb是针对linux内核中ubs驱动的一套api,会自行调用usb驱动接口,并为应用提供api接口,将usb设备开发减少为应用层开发 ...

  10. 基于PHP的cURL快速入门

    cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 cURL 库.本文将介绍 cURL 的一些高级特性,以及在PHP中如 ...