SystemVerilog Datatype: Class (Part 3)
Previous: Virtual methods |
Next: Classes with parameters
The Class Scope Resolution Operator ::
The class scope resolution operator :: is used for specifying an identifier
defined within the scope of a class. If My_class is a class that has a
static public property (or method) named my_x, then you can
access my_x from outside the class as My_class::my_x
It is worth inspecting the difference between the class scope resolution
operator (::) and object member reference operator (.). For instance,
if mc is an object instance of My_class, mc.my_x refers to the
member my_x of the instance mc, whereas, My_class::my_x
refers to the member of the class itself.
The :: operator in SystemVerilog applies to all static elements of a class.
These are:
- static properties
- static methods
- typedefs
- enumerations
- structures
- unions
- nested class declarations
There are few cases where the class scope resolution operator :: can be
particularly useful. These are described below.
(a) Accessing static properties and static methods from outside the
class hierarchy: Our previous example of My_class::my_x falls
under this category.
(b) Accessing public or protected base class members from within the sub
classes: Recall that from within a subclass, the members of the base
class (unless they are local) are fully accessible. This allows a
sub class (say, Trapezoid, in our previous example) to access a
member of the base class (in this case, Triangle) by using the ::
operator. The following example illustrates this.
class Trapezoid extends Triangle;
point p4;
...
task fillTrapezoid;
Triangle::fillIt();
FillTheRest(Traingle::p1,
Traingle::p2,
Traingle::p3,
p4);
endtask
...
task FillTheRest(struct point p1,
struct point p2,
struct point p3,
struct point p4);
... // details here
endtask
endclass
(c) Accessing an enumerated type or other named constants from outside
the scope: In the following example, triangle_type is an enumerated
type and printType() is a (publicly available) method within the
class Triangle. Outside the scope of the class, the following code
calls printType.
class Triangle;
typedef enum { isosceles
, equilateral
, others} triangle_type;
static task printType(triangle_type t);
... // details here
endtask
endclass
...
Triangle t = new;
...
Triangle::printType(Triangle::isosceles);
(d) Using an out of block method definition: The :: operator works
both ways - just as it is possible to access a method defined within a
class scope using the :: operator, it is also possible to declare a method
within a class scope as extern, and then define it outsde the scope.
In the next example, isFilled() is defined outside the scope of the
class Triangle.
class Traingle;
extern function bit isFilled();
...
endclass
function bit Tringle::isFilled();
// use Triangle::p1, Triangle::p2 to do the job
endfunction
Previous: Virtual methods |
Next: Classes with parameters
|