Difficulty: Easy · Puzzle 2 of 6 · Topic: Virtual Method Puzzles
Why does this class fail before simulation even starts, and what exact fix is required?
class base_cfg;
int id;
function new(int id);
this.id = id;
endfunction
endclass
class ext_cfg extends base_cfg;
function new();
// no super.new call
endfunction
endclassConstructors are not optional in inheritance when base has required parameters.
1) `base_cfg` has no zero-argument constructor; it requires `id`.
2) `ext_cfg::new` must call `super.new(<id>)` as the first statement.
3) Without it, compilation/elaboration fails due to unsatisfied base constructor call.Answer: It is illegal because base constructor needs an argument and `ext_cfg` never calls `super.new(id)`.
Not run here: The expected result is a compile-time error, and the harness treats a failed compile as a failed run. Checked by hand: Verilator 5.048 rejects the snippet with 'Missing argument on non-defaulted argument id in function call to FUNC new'.. The answer follows the language rules and the solution steps above.
The derived `new()` gets an implicit `super.new()` with no arguments, which cannot satisfy a required-argument base constructor. Candidates from other languages assume a default is synthesized.
How would you thread this constructor argument from UVM factory `create` path in components? Model answer: you cannot: `type_id::create(name, parent)` only passes `name` (and `parent` for components), so extra constructor arguments are not available. Give extra arguments defaults, or set them after `create` (or through `uvm_config_db` read in `build_phase`).
This is illegal, because the base constructor requires an argument and the extended constructor never calls super new with it. The base class has no zero-argument constructor, so the call has to be the first statement in the derived new. The trap is expecting an implicit default call to work. I would add super new with the id and then recompile.