Jump to content
Sign in to follow this  
jao16

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×