Skip to content

A subclass’s new constraint doesn’t replace the base class’s

write-from-scratch / inheritance / lifecycle-hook

Constraint blocks aren’t virtual methods. A differently named constraint in a subclass doesn’t override the base class’s: every block in the inheritance chain is ANDed together, so adding [0:200] on top of [0:50] still leaves [0:50].

constraint_mode(0) switches a named constraint block off, including an inherited one, without touching its declaration: rand_mode() for constraints instead of fields.

Fix the classes below.

RequirementsSo that wide_item’s value can reach above 50

  1. Add a constraint in wide_item allowing value up to 200.
  2. In wide_item’s constructor, turn off base_item’s c_range with constraint_mode(0).

Source on GitHub

inherited_constraints_stack.sv