Forward declaration

Forward declaration

In computer programming, a forward declaration is a declaration of a variable or function which the programmer has not yet given a complete definition.

int elements [] ;

void foo(int);

In C, the two lines above represent forward declarations of an array and a function of one parameter, respectively. (The latter is also a function prototype.) After processing these declarations, the compiler would allow the programmer to refer to the entities elements and foo in the rest of the program; but at some point the programmer would still have to provide "definitions" for the declared entities:

int elements [10] ;

void foo(int x) { printf("%d ", x);}

In Pascal and other Wirth programming languages, it is a general rule that all entities must be declared before use. In C, the same general rule applies, but with an exception for undeclared functions. Thus, in C it is possible (although unwise) to implement a pair of mutually recursive functions thus:

int first(int x) { if (x = 0) return 1; return second(x-1);}

int second(int x) { if (x = 0) return 0; return first(x-1);}

In Pascal, the same implementation requires a forward declaration of second to precede its use in first. Without the forward declaration, the compiler will produce an error message indicating that the identifier second has been used without being declared.

Forward reference

The term forward reference is sometimes used as a synonym of "forward declaration". [ [http://msdn2.microsoft.com/en-us/library/15k227ta(VS.71).aspx MSDN: Converting to a Forward-Reference Class Type] ] 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. [ [http://pages.cs.wisc.edu/~fischer/cs536.s07/lectures/Lecture25.4up.pdf] ] [ [http://www.codeguru.com/cpp/tic/tic0103.shtml Thinking in C++: Inlines & the compiler] ] Thus, we may say that because forward declarations are mandatory in Pascal, forward "references" are prohibited.

An example of (valid) forward reference in C++:

class C {public: void mutator(int x) { myValue = x; } int accessor() { return myValue; }private: int myValue;};

In this example, there are two references to myValue before it is declared. 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.

Permitting forward reference can greatly increase the complexity and memory requirements of a compiler, and generally prevents the compiler from being implemented in one pass.

References


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Declaration and address — Published December 1809 The Declaration and Address was written by Thomas Campbell in 1809. It was the founding document for the Christian Association of Washington, a short lived religious movement of the 19th century. The Christian Association… …   Wikipedia

  • Declaration of Breda — There are also two contemporary treaties known as the Treaty of Breda. The Declaration of Breda (issued on April 4, 1660) was a proclamation by Charles II of England in which he promised a general pardon for crimes committed during the English… …   Wikipedia

  • Declaration of Helsinki — This article is about the human medical experimentation ethics document. For other uses, see Declaration of Helsinki (disambiguation). The Declaration of Helsinki[1] is a set of ethical principles regarding human experimentation developed for the …   Wikipedia

  • Declaration on the Rights of Indigenous Peoples — Part of a series on Indigenous rights …   Wikipedia

  • Déclaration avancée — En programmation informatique, une déclaration avancée est une déclaration d un identificateur représentant une entité (par exemple un type, une variable, une fonction) pour laquelle la définition n est fournie qu ultérieurement dans le code.… …   Wikipédia en Français

  • Declaration of Independence (Israel) — Infobox document document name=Declaration of Independence|location of document= Tel Aviv writer=First Draft: Zvi Berenson Second Draft: Moshe Shertok David Remez Felix Rosenblueth Moshe Shapira Aharon Zisling Third Draft: David Ben Gurion Yehuda …   Wikipedia

  • Declaration of Independence — 1. the public act by which the Second Continental Congress, on July 4, 1776, declared the Colonies to be free and independent of England. 2. the document embodying it. * * * (July 4, 1776) Document approved by the Continental Congress that… …   Universalium

  • Declaration of Independence of Ukraine — Act of Declaration of Independence of Ukraine Ukrainian:Акт проголошення незалежності України Ratified 24 August 1991 Location …   Wikipedia

  • Declaration of Sentiments — The Declaration of Sentiments, also known as the Declaration of Rights and Sentiments,[1] is a document signed in 1848 by 68 women and 32 men, 100 out of some 300 attendees at the first women s rights convention, in Seneca Falls, New York, now… …   Wikipedia

  • Declaration of Independence of Lower Canada — The Declaration of Independence of Lower Canada was written in French by the patriot rebel Robert Nelson on February 22, 1838, while in exile in the United States, after the first rebellion of 1837. The 1838 declaration was primarily inspired by… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”