# How write a own template class which inherits from an existing vtk class

**URL:** https://discourse.vtk.org/t/how-write-a-own-template-class-which-inherits-from-an-existing-vtk-class/4195
**Category:** Support
**Created:** [September 11, 2020, 12:07am UTC](https://discourse.vtk.org/t/how-write-a-own-template-class-which-inherits-from-an-existing-vtk-class/4195 "2020-09-11T00:07:45Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amnesie/32/1673_2.png) [@Amnesie](https://discourse.vtk.org/u/Amnesie)
#### Post date: [September 11, 2020, 12:07am UTC](https://discourse.vtk.org/t/how-write-a-own-template-class-which-inherits-from-an-existing-vtk-class/4195/1 "2020-09-11T00:07:45Z")

</div>

Hi everyone,

I want to write a template class in vtk ,which inherits from an existing vtk class.

I use [this](https://vtk.org/Wiki/VTK/Examples/Cxx/IO/ReadDICOMSeries) ReadDICOMSeries as example.

Assuming I’m not sure the data type of `_ImageViewer`,maybe vtkResliceImageViewer or vtkImageViewer2.

So I should use a template class in this case .  
the class should look like this :

> template  
> class myVtkInteractorStyleImage : public vtkInteractorStyleImage  
> {  
> public:  
> static myVtkInteractorStyleImage\* New();  
> vtkTypeMacro(myVtkInteractorStyleImage, vtkInteractorStyleImage);
> 
> protected:  
> T1\* \_ImageViewer;  
> …  
> …  
> …  
> };  
> vtkStandardNewMacro(?)

> int main(){  
> …  
> …
> 
> ```
> vtkSmartPointer<myVtkInteractorStyleImage<vtkImageViewer2>> myInteractorStyle =
> vtkSmartPointer<myVtkInteractorStyleImage<vtkImageViewer2>>::New();
> 
> myInteractorStyle->SetImageViewer(imageViewer);
> ...
> ...
> }
> 
> ```

I have tried but i am strucked by how to define `vtkStandardNewMacro()`

Can someone help me?  
Thanks!

---

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amnesie/32/1673_2.png) [@Amnesie](https://discourse.vtk.org/u/Amnesie)
#### Post date: [September 16, 2020, 12:42am UTC](https://discourse.vtk.org/t/how-write-a-own-template-class-which-inherits-from-an-existing-vtk-class/4195/2 "2020-09-16T00:42:18Z")

</div>

5days 😭

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [September 16, 2020, 3:13am UTC](https://discourse.vtk.org/t/how-write-a-own-template-class-which-inherits-from-an-existing-vtk-class/4195/3 "2020-09-16T03:13:02Z")

</div>

My understanding is that templates are not permitted in public API of VTK classes, only in internal implementation (see [VTK coding standards](https://vtk.org/Wiki/VTK_Coding_Standards)).

This is not a limitation, but a really nice feature. Although templated classes can be easier to write and maintain for VTK _developers_, they are generally less convenient for VTK _users_ (templated implementations are more rigid, as you need to commit to specific types at compile time).

For example, look at how much simpler to use your vtkMyInteractorStyleImage class without templates in the public API:

```auto
vtkNew<vtkMyInteractorStyleImage> myInteractorStyle;
myInteractorStyle->SetViewer(someImageViewer);

```

Also see how flexible this class is - you can dynamically change between viewer classes at runtime, regardless of their type:

```auto
myInteractorStyle->SetViewer(someResliceImageViewer);

```

---

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amnesie/32/1673_2.png) [@Amnesie](https://discourse.vtk.org/u/Amnesie)
#### Post date: [September 16, 2020, 10:53am UTC](https://discourse.vtk.org/t/how-write-a-own-template-class-which-inherits-from-an-existing-vtk-class/4195/4 "2020-09-16T10:53:44Z")

</div>

Thanks! I didn’t know I couldn’t use template classes. From your reply, My understanding is that I can write 2 `SetViewer()` as overloading function, am I right?  
But if so ,how to initialize the member of class vtkMyInteractorStyleImage?  
Assume I wrote my own `InteractorStyle` class.  
The function of this style is when I move forward the mouse wheel then change the slice.  
The vtkMyInteractorStyleImage Class like this:

> class myVtkInteractorStyleImage : public vtkInteractorStyleImage  
> {  
> public:  
> static myVtkInteractorStyleImage\* New();  
> vtkTypeMacro(myVtkInteractorStyleImage, vtkInteractorStyleImage);  
> protected:  
> int \_Slice;  
> int \_MinSlice;  
> int \_MaxSlice;  
> vtkimageviewer2 \* \_ImageViewer;  
> public:  
> void Setviewer(vtkimageviewer2 \* Viewer){  
> \_ImageViewer = Viewer;  
> \_MinSlice = imageViewer-\>GetSliceMin();  
> \_MaxSlice = imageViewer-\>GetSliceMax();  
> \_Slice = \_MinSlice;  
> }
> 
> ```
> void MoveSliceForward() {
> if(_Slice < _MaxSlice) {
> _Slice += 1;
> cout << "MoveSliceForward::Slice = " << _Slice << std::endl;
> _ImageViewer->SetSlice(_Slice);
> _ImageViewer->Render();
> }
> }
> };
> vtkStandardNewMacro(myVtkInteractorStyleImage)
> 
> ```

For this class I would like to use for `vtkResliceImageViewer` as well.

How can I change the code to adapt to `vtkResliceImageViewer` class.?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [September 16, 2020, 12:21pm UTC](https://discourse.vtk.org/t/how-write-a-own-template-class-which-inherits-from-an-existing-vtk-class/4195/5 "2020-09-16T12:21:52Z")

</div>

It’ll look something like this:

```auto
void vtkMyInteractorStyleImage::MoveSliceForward()
{
  vtkResliceImageViewer* resliceImageViewer = vtkResliceImageViewer::SafeDownCast(this->ImageViewer);
  vtkImageViewer2* imageViewer = vtkImageViewer2::SafeDownCast(this->ImageViewer);
  if (resliceImageViewer)
  {
    resliceImageViewer->SetSlice(...);
    ...
  }
  else if (imageViewer)
  {
    imageViewer->SetSliceSomeOtherWay(...);
    ...
  }
  else
  {
    vtkErrorMacro("vtkMyInteractorStyleImage::MoveSliceForward failed: Invalid ImageViewer")
  }
};

```

---

<div class="post-metadata">

### Author: ![Amnesie](https://discourse.vtk.org/user_avatar/discourse.vtk.org/amnesie/32/1673_2.png) [@Amnesie](https://discourse.vtk.org/u/Amnesie)
#### Post date: [September 16, 2020, 2:36pm UTC](https://discourse.vtk.org/t/how-write-a-own-template-class-which-inherits-from-an-existing-vtk-class/4195/6 "2020-09-16T14:36:18Z")

</div>

`SafeDownCast(this->ImageViewer)` is the “key” .

I should have thought of that, thank you very much!
