# Feature: vtkObject inheritance without instantiation

**URL:** <https://discourse.vtk.org/t/feature-vtkobject-inheritance-without-instantiation/10426>\
**Category:** Development\
**Created:** [January 11, 2023, 8:55pm UTC](https://discourse.vtk.org/t/feature-vtkobject-inheritance-without-instantiation/10426 "2023-01-11T20:55:15Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcthomp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dcthomp/32/60_2.png) [@dcthomp](https://discourse.vtk.org/u/dcthomp)\
**Post date:** [January 11, 2023, 8:55pm UTC](https://discourse.vtk.org/t/feature-vtkobject-inheritance-without-instantiation/10426/1 "2023-01-11T20:55:15Z")

</div>

## Summary

The vtkObject class which serves as the basis for nearly all of VTK provides nice run-time methods like `GetClassName()` and `IsA()` to query objects for their type. These are implemented using the `Superclass` type-alias defined by `vtkTypeMacro()`. However, most of the methods are virtual overrides, making it hard to examine types without an instance of the object in question.

There are now some templated functions you can use to get at the same information without having an instance of a class, discussed the next section.

## Details

VTK now provides these free functions, templated on an object type to give you access to type information. They are all in the `vtk` namespace.

- [`vtk::TypeName<Object>()`](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/Core/vtkTypeName.h#L102-110) returns a `std::string` holding the name of the `Object` template-parameter. This is defined in `Common/Core/TypeName.h`. It works via the semi-standard `abi::__cxa_demangle()` call that most C++ compilers (including gcc, clang, icc, and msvc) provide.
- [`vtk::TypeToken<Object>()`](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/Core/vtkTypeName.h#L115-126) returns a [`vtkStringToken`](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/Core/vtkStringToken.h) holding an integer hash of the typename.  
Significant work is required to get uniform names across platforms, so it is unrealistic for this function to be `constexpr` until VTK allows c++14 and c++17 features.
- [`vtk::Inherits<VTKObject>(Container&)`](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/Core/vtkInherits.h) populates an STL container you pass (e.g., a vector or set) with all the typenames of an object by traversing the `Superclass` type-aliases. Because this function requires the type alias to be present, it generally only works on `vtkObject` and its subclasses.

See [TestInherits](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Common/Core/Testing/Cxx/TestInherits.cxx) for examples of their use.
