# Function new() with virtual keyword

**URL:** <https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, new-function, function-new\
**Created:** [February 11, 2021, 5:23am UTC](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708 "2021-02-11T05:23:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [February 11, 2021, 5:23am UTC](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708/1 "2021-02-11T05:23:19Z")

</div>

We implement constructor function new() in each class.  
Its defined as function with no return type.  
Why cant we specify it as virtual ?

Like

```verilog
virtual function new();

```

---

<div class="post-metadata">

**Author:** ![bl4ckp3rl](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bl4ckp3rl/32/2946_2.png) [@bl4ckp3rl](https://verificationacademy.com/forums/u/bl4ckp3rl)\
**Post date:** [February 11, 2021, 8:13am UTC](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708/2 "2021-02-11T08:13:28Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708):_

When we declare a function as virtual , the function will be registered to a loop up table at the time object is created using default constructor .When we reference that virtual function it will be found implicitly from the look up table.  
When we declare default constructor as virtual , the loop up table doesn’t exist yet for it ie even before object is constructed we are looking up the table. So the constructor function won’t be added to the LUT and hence it cant be declared as virtual.

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [February 11, 2021, 12:17pm UTC](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708/3 "2021-02-11T12:17:04Z")

</div>

_In reply to [bl4ckp3rl](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708/2):_

Hi Thanks for the reply.  
Why do we avoid mentioning void return type to new() constructor

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [February 11, 2021, 3:56pm UTC](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708/4 "2021-02-11T15:56:16Z")

</div>

_In reply to [bachan21](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708/3):_

> _In reply to [bl4ckp3rl](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708/3):_  
> Hi Thanks for the reply.  
> Why do we avoid mentioning void return type to new() constructor

Because it would be redundant.

This is patterned after other OOP languages like C++ and Java. The return type of the constructor can only be one type, the type of the class it is declared within. If you had to declare the return type, people might think you had a choice, which you don’t. Since new is a keyword, not a regular identifier, we can put this into the syntax.

---

<div class="post-metadata">

**Author:** ![bachan21](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/bachan21/32/1208_2.png) [@bachan21](https://verificationacademy.com/forums/u/bachan21)\
**Post date:** [February 12, 2021, 4:17am UTC](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708/5 "2021-02-12T04:17:24Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/function-new-with-virtual-keyword/38708/4):_

Thanks Dave
