From 91205663edce2c100faec1317fcf1cb33f5d7763 Mon Sep 17 00:00:00 2001 From: Niels Dekker <N.Dekker@lumc.nl> Date: Fri, 10 May 2024 14:57:17 +0200 Subject: [PATCH] STYLE: Remove `this->MakeOutput(0)` calls from constructors in Core MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In those cases, `MakeOutput(0)` just did `OutputType::New()` anyway. This commit avoids unnecessary casts and calls to virtual functions. Following C++ Core Guidelines, February 15, 2024, "Don’t call virtual functions in constructors and destructors", https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-ctor-virtual --- Modules/Core/Common/include/itkImageSource.hxx | 7 +++---- Modules/Core/Mesh/include/itkImageToMeshFilter.hxx | 7 +++---- Modules/Core/Mesh/include/itkMeshSource.hxx | 8 +++----- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/Modules/Core/Common/include/itkImageSource.hxx b/Modules/Core/Common/include/itkImageSource.hxx index 3103e4256eb..31dd68ea126 100644 --- a/Modules/Core/Common/include/itkImageSource.hxx +++ b/Modules/Core/Common/include/itkImageSource.hxx @@ -39,11 +39,10 @@ namespace itk template <typename TOutputImage> ImageSource<TOutputImage>::ImageSource() { - // Create the output. We use static_cast<> here because we know the default - // output must be of type TOutputImage - typename TOutputImage::Pointer output = static_cast<TOutputImage *>(this->MakeOutput(0).GetPointer()); this->ProcessObject::SetNumberOfRequiredOutputs(1); - this->ProcessObject::SetNthOutput(0, output.GetPointer()); + + // Equivalent to SetNthOutput(0, MakeOutput(0)); in this case, calling MakeOutput is not necessary. + this->ProcessObject::SetNthOutput(0, TOutputImage::New()); #if defined(ITKV4_COMPATIBILITY) m_DynamicMultiThreading = false; diff --git a/Modules/Core/Mesh/include/itkImageToMeshFilter.hxx b/Modules/Core/Mesh/include/itkImageToMeshFilter.hxx index dd52325c777..cacb5044978 100644 --- a/Modules/Core/Mesh/include/itkImageToMeshFilter.hxx +++ b/Modules/Core/Mesh/include/itkImageToMeshFilter.hxx @@ -27,11 +27,10 @@ template <typename TInputImage, typename TOutputMesh> ImageToMeshFilter<TInputImage, TOutputMesh>::ImageToMeshFilter() { this->ProcessObject::SetNumberOfRequiredInputs(1); - - OutputMeshPointer output = dynamic_cast<OutputMeshType *>(this->MakeOutput(0).GetPointer()); - this->ProcessObject::SetNumberOfRequiredOutputs(1); - this->ProcessObject::SetNthOutput(0, output.GetPointer()); + + // Equivalent to SetNthOutput(0, MakeOutput(0)); in this case, calling MakeOutput is not necessary. + this->ProcessObject::SetNthOutput(0, OutputMeshType::New()); } /** diff --git a/Modules/Core/Mesh/include/itkMeshSource.hxx b/Modules/Core/Mesh/include/itkMeshSource.hxx index b19e4e53ff2..a38054ac665 100644 --- a/Modules/Core/Mesh/include/itkMeshSource.hxx +++ b/Modules/Core/Mesh/include/itkMeshSource.hxx @@ -25,12 +25,10 @@ namespace itk template <typename TOutputMesh> MeshSource<TOutputMesh>::MeshSource() { - // Create the output. We use static_cast<> here because we know the default - // output must be of type TOutputMesh - OutputMeshPointer output = static_cast<TOutputMesh *>(this->MakeOutput(0).GetPointer()); - this->ProcessObject::SetNumberOfRequiredOutputs(1); - this->ProcessObject::SetNthOutput(0, output.GetPointer()); + + // Equivalent to SetNthOutput(0, MakeOutput(0)); in this case, calling MakeOutput is not necessary. + this->ProcessObject::SetNthOutput(0, TOutputMesh::New()); m_GenerateDataRegion = 0; m_GenerateDataNumberOfRegions = 0;