diff --git a/Modules/Core/Common/include/itkMacro.h b/Modules/Core/Common/include/itkMacro.h index 2208c96574e..a01303407c2 100644 --- a/Modules/Core/Common/include/itkMacro.h +++ b/Modules/Core/Common/include/itkMacro.h @@ -312,7 +312,7 @@ namespace itk Pointer smartPtr = ::itk::ObjectFactory::Create(); \ if (smartPtr == nullptr) \ { \ - smartPtr = new x; \ + smartPtr = new x(); \ } \ smartPtr->UnRegister(); \ return smartPtr; \ @@ -369,7 +369,7 @@ namespace itk #define itkFactorylessNewMacro(x) \ static Pointer New() \ { \ - x * rawPtr = new x; \ + x * rawPtr = new x(); \ Pointer smartPtr = rawPtr; \ rawPtr->UnRegister(); \ return smartPtr; \ diff --git a/Modules/Core/Common/test/CMakeLists.txt b/Modules/Core/Common/test/CMakeLists.txt index 1b0721bd3ee..4e08b209d27 100644 --- a/Modules/Core/Common/test/CMakeLists.txt +++ b/Modules/Core/Common/test/CMakeLists.txt @@ -1734,6 +1734,7 @@ set(ITKCommonGTests itkImageRegionGTest.cxx itkIndexGTest.cxx itkIndexRangeGTest.cxx + itkLightObjectGTest.cxx itkMakeUniqueForOverwriteGTest.cxx itkMatrixGTest.cxx itkMersenneTwisterRandomVariateGeneratorGTest.cxx diff --git a/Modules/Core/Common/test/itkLightObjectGTest.cxx b/Modules/Core/Common/test/itkLightObjectGTest.cxx new file mode 100644 index 00000000000..3fece1521cf --- /dev/null +++ b/Modules/Core/Common/test/itkLightObjectGTest.cxx @@ -0,0 +1,82 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +// First include the header file to be tested: +#include "itkLightObject.h" +#include "itkObjectFactory.h" +#include + + +// Tests that New() does initialize the data of a derived class, even when the derived class has a defaulted +// default-constructor *and* its data has no {} initializer. Uses itkSimpleNewMacro to define New(). +TEST(LightObject, SimpleNewInitializesDataOfDerivedClass) +{ + class DerivedClass : public itk::LightObject + { + public: + ITK_DISALLOW_COPY_AND_MOVE(DerivedClass); + + using Self = DerivedClass; + using Pointer = itk::SmartPointer; + + itkSimpleNewMacro(DerivedClass); + itkGetConstMacro(Data, int); + + protected: + // Defaulted default-constructor, essential to this test. + DerivedClass() = default; + + // Defaulted destructor. + ~DerivedClass() override = default; + + private: + int m_Data; // Without {} initializer, for the purpose of this test. + }; + + EXPECT_EQ(DerivedClass::New()->GetData(), 0); +} + + +// Tests that New() does initialize the data of a derived class, even when the derived class has a defaulted +// default-constructor *and* its data has no {} initializer. Uses itkFactorylessNewMacro to define New(). +TEST(LightObject, FactorylessNewInitializesDataOfDerivedClass) +{ + class DerivedClass : public itk::LightObject + { + public: + ITK_DISALLOW_COPY_AND_MOVE(DerivedClass); + + using Self = DerivedClass; + using Pointer = itk::SmartPointer; + + itkFactorylessNewMacro(DerivedClass); + itkGetConstMacro(Data, int); + + protected: + // Defaulted default-constructor, essential to this test. + DerivedClass() = default; + + // Defaulted destructor. + ~DerivedClass() override = default; + + private: + int m_Data; // Without {} initializer, for the purpose of this test. + }; + + EXPECT_EQ(DerivedClass::New()->GetData(), 0); +}