# Simplifying the use of vtkNew

**URL:** https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560
**Category:** Development
**Created:** [June 23, 2017, 3:44pm UTC](https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560 "2017-06-23T15:44:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [June 23, 2017, 3:44pm UTC](https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560/1 "2017-06-23T15:44:00Z")

</div>

For C++ developers only. We had a discussion on the VTK mailing list arguing that vtkNew should be as simple to use as vtkSmartPointer or not. Currently, when an object is created by vtkNew, you need to call .GetPointer() to get the raw pointer.

Current usage:

```
someFilter->SetInputData(imageData.GetPointer());

```

I recommended to add an implicit converter to raw pointer, so that we can simply write:

```
someFilter->SetInputData(imageData);

```

If you agree that it would be better to simplify the syntax, **[please mark your preference in this poll - takes two clicks](https://docs.google.com/forms/d/e/1FAIpQLScTeGsjR925mL7KyNeTHB3-tvmYxaJF0_72ZnQgX1ihbJuX4A/viewform)**. If you think that requiring adding .GetPointer() is safer, fill the form accordingly, too.

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [June 23, 2017, 4:40pm UTC](https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560/2 "2017-06-23T16:40:28Z")

</div>

It that the main use case? Maybe the vtkSetObjectMacro should be polymorphic and apply GetPointer when needed.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [June 23, 2017, 6:21pm UTC](https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560/3 "2017-06-23T18:21:41Z")

</div>

I think we could add implicit conversion for existing classes at Slicer level, but that would mean that VTK code that works in Slicer may not work (or work slightly differently) in a plain VTK environment. So far 75% of poll respondents support this simplification, so hopefully this can be changed in VTK.

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [December 13, 2018, 3:47pm UTC](https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560/4 "2018-12-13T15:47:21Z")

</div>

@lassoan I thought we were getting rid of GetPointer, but it’s still needed, at least on my gcc 7.3 ubuntu 18.04 builds. I have been [just fixing them](https://github.com/Slicer/Slicer/commit/8f47ce8cb596de776a70ef3731fc4244be909a93) but they keep popping up. Can we come up with some way to get rid of them all or if not can we explicitly always use them.

```auto
[11%] Building CXX object Libs/MRML/Core/CMakeFiles/MRMLCore.dir/vtkMRMLSegmentationNode.cxx.o
/home/pieper/slicer4/latest/Slicer/Libs/MRML/Core/vtkMRMLSegmentationNode.cxx: In member function ‘virtual bool vtkMRMLSegmentationNode::GenerateEditMask(vtkOrientedImageData*, int, vtkOrientedImageData*, std:: __cxx11::string, std::__ cxx11::string, vtkOrientedImageData*, double*, vtkMRMLSegmentationDisplayNode*)’:
/home/pieper/slicer4/latest/Slicer/Libs/MRML/Core/vtkMRMLSegmentationNode.cxx:782:63: error: no matching function for call to ‘vtkOrientedImageData::SetImageToWorldMatrix(vtkNew<vtkMatrix4x4>&)’
   maskImage->SetImageToWorldMatrix(referenceImageToWorldMatrix);

```

---

<div class="post-metadata">

### Author: ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)
#### Post date: [December 13, 2018, 4:02pm UTC](https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560/5 "2018-12-13T16:02:44Z")

</div>

After the next patch release, we should be able to remove the explicit use of `GeTPointer()`

> at least on my gcc 7.3 ubuntu 18.04 builds

Could you confirm that the build is done with VTK8 and c++11 ?

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [December 13, 2018, 4:49pm UTC](https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560/6 "2018-12-13T16:49:56Z")

</div>

@pieper I don’t know about any other developer using qt4/vtk7, so it would be great if you could keep fixing these. I try to pay attention to adding those GetPointer() calls but since the compiler does not complain, it us easy to miss them. After releasing 4.10.1, qt4/vtk7 will not be supported, so this will not be problem anymore.

---

<div class="post-metadata">

### Author: ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)
#### Post date: [December 15, 2018, 12:31am UTC](https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560/7 "2018-12-15T00:31:13Z")

</div>

I added the extra GetPointer calls and also version check ifdef on one vtk method that was added after vtk7.

This was actually a vtk7/Qt5.8 build. I’ve stopped doing Qt4 builds and have no reason to keep a vtk7 build (it just happened that I updated an old build tree). So if we can’t be sure these work we should explicitly stop supporting them.

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [December 15, 2018, 5:57am UTC](https://discourse.slicer.org/t/simplifying-the-use-of-vtknew/560/8 "2018-12-15T05:57:24Z")

</div>

We’ll explicitly stop supporting it in Slicer-5, which I think should be now or within a few days.
