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


Tutorials



THIS

Using The This Keyword

    Within an instance method or a constructor, this is a reference to the current object , the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
    
    The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.

    For example, in the packet class was written like this

EXAMPLE
   class packet ;
        int length = 0;
        //constructor
       function new (int l);
        length = l;
       endfunction
    endclass


    but it could have been written like this:

EXAMPLE:
   class packet ;
       int length = 0;
        //constructor
       function new (int length);
           this.length = length;
       endfunction
    endclass

    
    program main;
    
        initial
        begin
            packet pkt;
            pkt =new(10);
            $display(pkt.length);
        end
    
    endprogram

RESULT

  10



    Each argument to the second constructor shadows one of the object's fields inside the constructor "length" is a local copy of the constructor's first argument. To refer to the legnth field inside the object , the constructor must use "this.length".


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