Constructing sub-components in constructor VS in build_phase

Hi ,

I see that sub-components could be created inside the constructor but in all codes/examples I see that build_phase is used .

Why is it so ?

I read following lines regarding the same but couldn’t understand it ::


Allocate all sub-components in the build_phase rather than the constructor allows component to be configured before it allocates the hierarchy underneath it . Since constructor isn't polymorphic , it is not possible for a derivative class to change the structure unless the structure is created in the build function .

Need help with the above lines .

Also config_db;;get() is widely used in build_phase , could using “this” as argument to config_db::get() called inside the constructor cause any issues ?

In reply to MICRO_91:

When extending a class, you can only append statements to the constructor since calling super.new() must be the first statement of any class constructor except for the base class

class A;
  function new();
    statement1;
  endfunction
endclass
class B extends A;
  function new();
    super.new();
    statement2;
  endfunction
endclass

This always results in executing statement1 followed by statement2. But for any other kind of method, you have a choice of where you want to put the call to super.method, or if you want to call it at all.

class
``` verilog
class A;
  function new();
    statement1;
  endfunction
  virtual function void method();
    statement1;
  endfunction
endclass
class B extends A;
  function new();
    super.new();
    statement2;
  endfunction
  virtual function void method();
    statementPre;
    if (condition)
       super.method();
    statementPost;
  endfunction
endclass

This comes in very handy when you want to completely replace the build_phase() with new code, not just add to it.