Jump to content

When to use create() vs new()


Recommended Posts

Hello

Hello all

I am defining a test library as such:

package my_test_lib_pkg;

class my_test_base extends uvm_test;

`uvm_component_utils(my_test_base)

// declare an instance of the configuration, environment and agent classes

my_cfg cfg_0;

my_env env_0;

my_agent agent_0;

// constructor

function new(string name = "my_test_base", uvm_component parent_null);

super.ew(name, parent);

endfunction : new

// Now defining the build() phase.

// In the build() phase, i would generate the above classes as such:

function void build();

cfg_0 = my_cfg::type_id::create("cfg_0", this);

env_0 = my_env::type_id::create("env_0", this);

agent_0 = my_agent::type_id::create("agent_0", this);

endfunction : build

However, in some case i have seen the following code to generate the required instances:

cfg_0 = new();

env_0 = new();

agent_0 = new();

So my question is

a) what is the difference between create() and new() ?

B) when should one use create() instead of new() and vice versa

Thanks in advance

JO

Link to comment
Share on other sites

Use create() if you will ever want to use the factory to override the base class. If you new() your classes, you won't have the option of later using the factory without changing the code.

We use create() for everything*, because you never know when you'll want to override the base class to do something else. It adds a little bit of overhead, but it's usually worth it.

*I don't use create for TLM instances. Doesn't seem likely that I'll ever override those.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...