FACTOID # 61: Indonesia contains the most known mammal species - and the most mammal species under threat.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Circular dependency

Circular dependencies is a situation which occurs in object oriented programming when two or more objects point towards each other in a circular fashion.


Circular dependencies are usually used to implement callback functionality in programs. However, occurrences of circular dependencies should normally be limited, since they make the resulting object-models unclear and unstructured, and are hence considered a bad programming habit. Circular dependencies will also prevent some very primitive automatic garbage collectors (those that use reference counting) from deallocating objects.


Circular dependencies in C++

Implementation of circular dependencies in C/C++ can be a bit tricky, due to the requirement that any class or structure definition must be placed above their usage in the same file. A circular dependency between classes A and B will thus both require the definition of A to be placed above B, and the definition of B to be placed above A, which of course is impossible. A forward declaration trick is therefore needed to accomplish this. To meet Wikipedias quality standards, this article or section may require cleanup. ...


Circular dependencies in C/C++ are implemented by using forward declarations. The following example illustrates how this is done.

  • File a.h:
 #ifndef A_H #define A_H class B; //forward declaration class A { public: B* b; }; #endif //A_H 
  • File b.h:
 #ifndef B_H #define B_H class A; //forward declaration class B { public: A* a; }; #endif //B_H 
  • File main.cpp:
 #include "a.h" #include "b.h" void main() { A a; B b; a.b = &b; b.a = &a; } 

Self-reference example

Following is another example of forward declaration, which might be useful if the application needs a self-sustaining array of objects which is able to add and remove objects from itself during run-time:

  • File a.h:
 class A { public: static A *first, *last; A *previous, *next; A(); ~A(); }; 

The static variables first and last have to be defined, because their declaration does not reserve memory space for them. Note: static variables do not change from object to object and stay the same for this given class.

  • File a.cpp:
 #include "a.h" A *A::first, *A::last; // don't put the word static here, that will cause an error A::A() { previous=last; previous->next=this; last=this; next=0; } A::~A { previous->next=next; next->previous=previous; } 

  Results from FactBites:
 
Project Analyzer Help - File dependency analysis (968 words)
This may be due to a lot of files depending on a central file, which may contain the main form of the project, or an important class or module.
Circular dependencies are the pathological dependency structure that should be avoided.
Circular dependency index (CDI) is a metric that evaluates the degree of circular dependencies in a system.
Circular definition - Wikipedia, the free encyclopedia (276 words)
A circular definition is one that assumes a prior understanding of the term being defined.
A circular definition occurred in an early definition of the kilogram.
Circular sets are good for modelling cycles and, despite the field's name, this area of mathematics is well founded.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m