# vtk::MakeSmartPointer(T\*), vtk::TakeSmartPointer(T\*)

**URL:** <https://discourse.vtk.org/t/vtk-makesmartpointer-t-vtk-takesmartpointer-t/1287>\
**Category:** Development\
**Tags:** proposal\
**Created:** [July 5, 2019, 10:08pm UTC](https://discourse.vtk.org/t/vtk-makesmartpointer-t-vtk-takesmartpointer-t/1287 "2019-07-05T22:08:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![allison.vacanti](https://discourse.vtk.org/user_avatar/discourse.vtk.org/allison.vacanti/32/679_2.png) [@allison.vacanti](https://discourse.vtk.org/u/allison.vacanti)\
**Post date:** [July 5, 2019, 10:08pm UTC](https://discourse.vtk.org/t/vtk-makesmartpointer-t-vtk-takesmartpointer-t/1287/1 "2019-07-05T22:08:53Z")

</div>

One of the remaining tedious parts of using `vtkSmartPointer` is their creation. For instance, consider creating a new `vtkCompositeDataSetIterator` from `vtkCompositeDataSet::NewIterator()` and putting it into a smart pointer. There are two ways of doing this currently:

```auto
vtkSmartPointer<vtkCompositeDataSetIterator> cdsIter;
cdsIter.TakeReference(cds->NewIterator)();

```

or

```auto
auto cdsIter = 
  vtkSmartPointer<vtkCompositeDataSetIterator>::Take(cds->NewIterator());

```

[This merge request](https://gitlab.kitware.com/vtk/vtk/merge_requests/5718) adds a couple of new functions that are similar to `std::make_shared`, but for `vtkSmartPointer` and `vtkWeakPointer`. This allows the above examples to be written much more concisely:

```auto
// Equivalent to the above examples.
auto cdsIter = vtk::TakeSmartPointer(cds->NewIterator());

```

The new methods are:

- `vtk::TakeSmartPointer(T *obj)` takes an instance of type `T` (a subclass of `vtkObject`), and returns a `vtkSmartPointer<T>` without adding a new reference. That is, it “takes” the existing reference.
- `vtk::MakeSmartPointer(T *obj)` is similar, but it “makes” a new reference to `obj`.
- `vtk::TakeWeakPointer(T *obj)` creates a new `vtkWeakPointer<T>` that references `obj`.

I’ve found these (especially `TakeSmartPointer` to be very useful, would others like to see these added? Comments and reviews are welcome.

---

<div class="post-metadata">

**Author:** ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)\
**Post date:** [August 22, 2019, 12:47am UTC](https://discourse.vtk.org/t/vtk-makesmartpointer-t-vtk-takesmartpointer-t/1287/2 "2019-08-22T00:47:57Z")

</div>

I’ve never needed more methods for vtkSmartPointer, but then again I am known as “Simple Bill.”
