Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward reference vs. forward declaration

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"?

like image 501
mslot Avatar asked Mar 30 '09 09:03

mslot


People also ask

What do you mean by forward declaration?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition.

What is the purpose of forward declaration?

A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier. In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function's body.

Can you forward-declare reference?

Can I forward declare them? Yes, but in that case you can only use a reference or a pointer to the forward-declared class.

Should I use forward declaration or include?

As the name itself implies, forward declaration is just a Declaration and not a definition. So, you will declare saying the compiler that it is a class and I just declaring it here and will provide you the definition when am gonna use it. So, normally you forward declare in the Header file and #include in the .

What is forward declaration in C++?

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.

What is a forward reference in Pascal?

Forward reference. However, more often it is taken to refer to the actual use of an entity before any declaration; that is, the first reference to second in the code above is a forward reference. Thus, we may say that because forward declarations are mandatory in Pascal, forward references are prohibited.

What is a forward reference error?

Using an identifier before its declaration is called a forward reference, and results in an error, except in the following cases: When a goto statement refers to a statement label before the label's declaration. When a structure, union, or enumeration tag is used before it is declared. What is see reference? Answer: What is see reference?

Are forward references allowed in C++?

C++ generally prohibits forward references, but they are allowed in the special case of class members. Since the member function accessor cannot be compiled until the compiler knows the type of the member variable myValue, it is the compiler's responsibility to remember the definition of accessor until it sees myValue 's declaration.


2 Answers

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."

like image 144
Rose Perrone Avatar answered Sep 30 '22 22:09

Rose Perrone


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.

like image 44
Prabhu. S Avatar answered Sep 30 '22 22:09

Prabhu. S