Do Constraints behave as virtual

Hi all ,

I have the following code ::



class Base ;

rand bit [2:0] b ;

constraint B { b == 0 ; }


endclass

class Ext1 extends Base ;

constraint B { b == 1 ; } // Override Constraint "B" 


endclass

Base b ;

Ext1 e1 ;

initial begin

e1 = new();
  b  = e1 ;
  b.B.constraint_mode(0); // If it behaves as Virtual it would turn-off Constraint B in Class Ext1
  
  repeat(3)
  if ( e1.randomize() )
  begin
     
     $display(" Success with %p as Constraint Block B is %s ",b, e1.B.constraint_mode() == 1 ? "ON" : "OFF");
  
  end


end


Since Base class can’t point to extended class properties and constraint blocks are class members ,

Should it turn-off Constraint B in extended class ? If yes , won’t this make Constraint Blocks similar to a Virtual method ?

Since you declared constraint of derieved class with same constraint name it has more say over parent . Basically it overides the parent constraint .

In reply to Sv-hustler:

Yes that part I understand .

Question is whether b.B.constraint_mode(0) points to Constraint in object e1 ? .

Is yes isn’t that illegal since base class can’t point to extended class properties ?

In reply to Have_A_Doubt:

Constraints are not virtual, the randomize() method is. When you declare a constraint with the same name in an extended class, you hide the original constraint from randomize. You are still able to access the constraint_mode of both constraints, but only the mode of the extended constraint will have an effect. The base constraint is not visible.

Just want to revisit this topic and have a little bit more clarification.

class base;
    randc int a;
    constraint d { a < 10;}
    virtual function void display();
        $display("(%m) %0d",a);
        post_display();
    endfunction
    function post_randomize();
        a+=100;
    endfunction
    function void post_display();
        $display("base post_display");
    endfunction
endclass

class ext extends base;
    constraint d { (a > 10) && (a < 20) ;}
    function void display();
        $display("(%m) %0d",a);
        post_display();
    endfunction
    function post_randomize();
        a+=200;
    endfunction
    function void post_display();
        $display("ext post_display");
    endfunction
endclass

module top();
base b1;
ext e1;
initial begin
    e1 = new();
    void'(e1.randomize());
    e1.display();

    b1 = e1;
    void'(b1.randomize());
    b1.display();
end
endmodule

The above code prints the result as,

($unit_0x42828247.ext.display) 217
ext post_display
($unit_0x42828247.ext.display) 218
ext post_display

Here what I observe is that even though constraints and the function “post_display” are not virtual, because of the fact that these are called from virtual methods, they somehow act as virtual themselves in this context?
Finding these concepts a little bit confusing.
Shoudnt the post_display() call from base handle object point to the base handle definition of post_display() ?