# How to understand this use of New() within a struct?

**URL:** https://discourse.vtk.org/t/how-to-understand-this-use-of-new-within-a-struct/14412
**Category:** Support
**Created:** [August 13, 2024, 10:59pm UTC](https://discourse.vtk.org/t/how-to-understand-this-use-of-new-within-a-struct/14412 "2024-08-13T22:59:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [August 13, 2024, 10:59pm UTC](https://discourse.vtk.org/t/how-to-understand-this-use-of-new-within-a-struct/14412/1 "2024-08-13T22:59:53Z")

</div>

I’ve usually seen New() used as follows:  
`vtkPolyData *pd = vtkPolyData::New();`

But I’ve encountered a syntax I haven’t seen before: what does New() do in the following struct?

```auto
struct MyItem {

  struct Data : vtkObject
  {
    static Data* New(); // static pointer referencing itself??
  };
};

```

It seems that struct Data contains a static reference to an instance of itself - but why is it declared in that ‘self referential’ way? It’s obviously valid C++ since it compiles, but can someone explain the syntax for me?

… and later in the code:

```auto
// Initialize the static Data object - but what is happening in the struct Data definition?
`vtkStandardNewMacro(MyItem::Data);`

```

Thanks!

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [August 13, 2024, 11:59pm UTC](https://discourse.vtk.org/t/how-to-understand-this-use-of-new-within-a-struct/14412/2 "2024-08-13T23:59:28Z")

</div>

VTK objects are created using the object factory pattern.

> [@Tomasso](#):
>
> ```auto
> static Data* New(); // static pointer referencing itself??
> 
> ```

In C++, that is a declaration of a function which can be called as a normal function i.e, without needing an instance of the struct itself.

> [@Tomasso](#):
>
> ```auto
> // Initialize the static Data object - but what is happening in the struct Data definition?
> `vtkStandardNewMacro(MyItem::Data);`
> 
> ```

That comment is completely wrong. It does not “initialize” the static data object (whatever that is). It creates a new instance of `MyItem::Data`.

The `vtkStandardNewMacro` expands to define the `New` method such that it constructs and returns an instance of `MyItem::Data`. It does not do any kind of initialization. The reason for the `New` method is to allow consumers to extend VTK by implementing object factory overrides for VTK classes.

Some classes in VTK use `vtkObjectFactoryNewMacro` which creates a new instance using the `vtkObjectFactory`.

---

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [August 14, 2024, 4:36pm UTC](https://discourse.vtk.org/t/how-to-understand-this-use-of-new-within-a-struct/14412/3 "2024-08-14T16:36:05Z")

</div>

Thanks Jaswant!  
So the struct Data declares a static New() element:  
` static Data* New(); `

And the following _defines_ the Data::New() function?

`vtkStandardNewMacro(MyItem::Data);`

If so, does the above actually get executed and instantiate a new Data object on the heap before main() is called, even if the code never calls Data::New() anywhere else? I.e. if code in main() does this:

`vtkNew(MyItem::Data) vtk;`

Are there now _two_ instances of MyItem::Data on the heap?

Thanks!

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [August 14, 2024, 5:17pm UTC](https://discourse.vtk.org/t/how-to-understand-this-use-of-new-within-a-struct/14412/4 "2024-08-14T17:17:18Z")

</div>

> [@Tomasso](#):
>
> So the struct Data declares a static New() element:

No, not an element, it declares the `New` function.

> [@Tomasso](#):
>
> And the following _defines_ the Data::New() function?
> 
> `vtkStandardNewMacro(MyItem::Data);`

Correct

> [@Tomasso](#):
>
> If so, does the above actually get executed and instantiate a new Data object on the heap before main() is called, even if the code never calls Data::New() anywhere else? I.e. if code in main() does this

No, it’s instantiated only when `New` is invoked like

```cpp
MyItem::Data* instance = MyItem::Data::New();
// or using smartpointer
auto instance = vtk::TakeSmartPointer(MyItem::Data::New());
// or using vtkNew
vtkNew<MyItem::Data> instance;

```

> [@Tomasso](#):
>
> vtkNew(MyItem::Data) vtk;

I don’t understand what this means.

---

<div class="post-metadata">

### Author: ![Tomasso](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tomasso/32/5786_2.png) [@Tomasso](https://discourse.vtk.org/u/Tomasso)
#### Post date: [August 14, 2024, 5:33pm UTC](https://discourse.vtk.org/t/how-to-understand-this-use-of-new-within-a-struct/14412/5 "2024-08-14T17:33:11Z")

</div>

> [@Tomasso](#):
>
> vtkNew(MyItem::Data) vtk;

Oops, should be  
`vtkNew<MyItem::Data> vtk;`

Thanks - it all makes more sense now… I’m still getting accustomed to extensive macro use in C++!
