|HOME |ABOUT |ARTICLES |ACK |FEEDBACK |TOC |LINKS |BLOG |JOBS |


Tutorials



TYPEDEF CLASS


Forward Reference



A forward declaration is a declaration of a object which the programmer has not yet given a complete definition. The term forward reference is sometimes used as a synonym of forward declaration. However, more often it is taken to refer to the actual use of an entity before any declaration. The SystemVerilog language supports the typedef class construct for forward referencing of a class declaration. This allows for the compiler to read a file from beginning to end without concern for the positioning of the class declaration.


EXAMPLE:
class Y ;
X x; // refers to Class X, which is not yet defined
endclass

class X;
int i;
endclass

RESULT

Error : Class X is not defined




When the compiler encounters the handle x of class type X referred to in class Y, it does not yet know the definition for class X since it is later in the file. Thus, compilation fails.
To rectify this situation, typedef is used to forward reference the class declaration.



EXAMPLE:
typedef class X;
class Y ;
X x; // refers to Class X, which is not yet defined
endclass

class X;
int i;
endclass



The typedef of class X allows the compiler to process Y before X is fully defined. Note that typedef cannot be used to forward reference a class definition in another file. This must be done using the inclusion of a header file.



Circular Dependency



A Circular dependency is a situation which can occur in programming languages wherein the definition of an object includes the object itself. One famous example is Linked List.



EXAMPLE:
class Y ;
int i;
Y y; // refers to Class Y, which is not yet defined
endclass



As you seen, there is a compilation error. To avoid this situation, typedef is used to forward reference the class declaration and this circular dependency problem can be avoided.


Index
Introduction
Class
Object
This
Inheritance
Encapsulation
Polymorphism
Abstract Classes
Parameterised Class
Nested Classes
Constant
Static
Casting
Copy
Scope Resolution Operator
Null
External Declaration
Classes And Structures
Typedef Class
Pure
Other Oops Features
Misc

Report a Bug or Comment on This section - Your input is what keeps Testbench.in improving with time!





<< PREVIOUS PAGE

TOP

NEXT PAGE >>

copyright 2007-2017 :: all rights reserved www.testbench.in::Disclaimer