# GetModifiable and GetConst Member Functions

Hans Johnson
2013-02-18
ENH: Get function accessible from const objects

This improves the safety of getting a smart pointer to an internal ivar itk::Object. Creates the member functions Get"name"() (e.g., GetPoints()) for the read only access, and GetModifiable"name"() for read/write access.

NOTE: The GetModifiable"name"() function returns a non-const version of the internal member variable and could easily be used to modify the behavior of the class without properly resetting the pipeline symantics

NOTE: A class can use either itkGetModifiableObjectMacro or itkGetObjectMacro, but not both. A class can use either itkGetModifiableObjectMacro or itkGetConstObjectMacro, but not both. If the desired behavior is to only provide const access to the itkObject ivar, then use itkGetConstObjectMacro, else use itkGetModifiableObjectMacro for read/write access to the ivar. It is permissable to use both itkGetObjectMacro and itkGetConstObjectMacro for backwards compatibility. If the ITK_LEGACY_REMOVE=FALSE, then it is permissable to use itkGetObjectMacro which defines both signatures itk::GetXXX() and itk::GetModifiableXXX()

The Get methods for functions need to have version that is accessible from const versions of the object.

In most cases access to the internal member variables for read-only purposes is accomplished through a const Get member function.

The most common paradigm used to preserve the principle of encapsulation for class member functions is to provide a Set function, and a const Get function. In ITK we routinely provide a Get function, but the one that spelled similar to the itkSetObjectMacro is the itkGetObjectMacro which is not what is normally desired. Normally one should use the itkGetConstObjectMacro to preserve encapsulation, and only use itkGetObjectMacro in cases where it is necessary to allow changes the class outside that classes member functions (i.e. for performance reasons.

Allow explicit request for modifiable Get*

When using a Get* member function to get access to and internal member variable, the standard itkGetObjectMacro returned a pointer that was modifiable. This allow breaking the rule of encapsulation where member variables state should only be set through class member functions. By doing this, it is easy to change the internal state of a ProcessObject without changing that ProcessObjects Modified status.

A const version of the itkGetConstObjectMacro is complemented by a itkGetModifiableObjectMacro with a different name to explicitly convey that the returned object could be modified without following standard ITK symantic of updating the ProcessObject Modified status.

The itkGetModifiableObjectMacro provides the same behavior as the historical itkGetObjectMacro (i.e. it returns a non-const pointer to the internal member variable). The problem with using itkGetObjectMacro and itkGetConstObjectMacro is that the non-const version almost always took precidence, even when the const version would suffice to accomplish the needed task.

This patch set has completely removed the historical itkGetObjectMacro to determine the portability of the current code that only has itkGetConstObjectMacro and itkGetModifiableObjectMacro. The ubiquitous nature of the itkGetObjectMacro in 3rd party applications means that we MUST preserve this function for backwards compatibility for a long forseeable future. I would propose that an advanced option be made available to allow disabling this function for those developers who want to fortify their code with maximal use of const functions could disable the itkGetObjectMacro and force use of either itkGetConstObjectMacro or itkGetModifiableObjectMacro.

registration-&gt;GetOptimizer() );
registration-&gt;GetModifiableOptimizer() );
itkGetObjectMacro();
itkGetModifiableObjectMacro();
I8fbdf8cc999c78292ed167b83c253a6b3aded763
Examples/Registration/MultiResImageRegistration1.cxx Examples/Registration/MultiResImageRegistration2.cxx Examples/Registration/MultiResImageRegistration3.cxx Modules/Compatibility/V3Compatibility/include/itkDeformationFieldSource.h Modules/Compatibility/V3Compatibility/include/itkDeformationFieldSource.hxx Modules/Compatibility/V3Compatibility/include/itkInverseDeformationFieldImageFilter.h Modules/Compatibility/V3Compatibility/include/itkInverseDeformationFieldImageFilter.hxx Modules/Core/Common/include/itkBinaryThresholdSpatialFunction.h Modules/Core/Common/include/itkDomainThreader.h Modules/Core/Common/include/itkImageDuplicator.h Modules/Core/Common/include/itkMacro.h Modules/Core/Common/include/itkProgressAccumulator.h Modules/Core/Common/include/itkStreamingImageFilter.h Modules/Core/Common/test/itkStreamingImageFilterTest.cxx Modules/Core/GPUCommon/include/itkGPUImage.h Modules/Core/GPUCommon/include/itkGPUImageDataManager.h Modules/Core/ImageFunction/include/itkCentralDifferenceImageFunction.h Modules/Core/ImageFunction/include/itkRayCastInterpolateImageFunction.h Modules/Core/Mesh/include/itkInteriorExteriorMeshFilter.h Modules/Core/Mesh/include/itkTransformMeshFilter.h Modules/Core/Mesh/test/itkMeshSourceGraftOutputTest.cxx Modules/Core/SpatialObjects/include/itkAffineGeometryFrame.h Modules/Core/SpatialObjects/include/itkGaussianSpatialObject.hxx Modules/Core/SpatialObjects/include/itkImageSpatialObject.h Modules/Core/SpatialObjects/include/itkImageSpatialObject.hxx Modules/Core/SpatialObjects/include/itkMetaGroupConverter.hxx Modules/Core/SpatialObjects/include/itkPolygonGroupSpatialObject.hxx Modules/Core/SpatialObjects/include/itkSpatialObject.h Modules/Core/SpatialObjects/include/itkSpatialObject.hxx Modules/Core/SpatialObjects/include/itkSpatialObjectDuplicator.h Modules/Core/SpatialObjects/include/itkSpatialObjectTreeContainer.hxx Modules/Core/SpatialObjects/test/itkAffineGeometryFrameTest.cxx Modules/Core/SpatialObjects/test/itkEllipseSpatialObjectTest.cxx Modules/Core/SpatialObjects/test/itkGaussianSpatialObjectTest.cxx Modules/Core/Transform/include/itkBSplineBaseTransform.h Modules/Core/Transform/include/itkKernelTransform.h Modules/Core/Transform/test/itkSplineKernelTransformTest.cxx Modules/Filtering/BiasCorrection/include/itkMRIBiasFieldCorrectionFilter.h Modules/Filtering/Colormap/include/itkScalarToRGBColormapImageFilter.h Modules/Filtering/Colormap/include/itkScalarToRGBColormapImageFilter.hxx Modules/Filtering/Colormap/test/itkScalarToRGBColormapImageFilterTest.cxx Modules/Filtering/Deconvolution/include/itkIterativeDeconvolutionImageFilter.h Modules/Filtering/Deconvolution/include/itkParametricBlindLeastSquaresDeconvolutionImageFilter.h Modules/Filtering/Denoising/include/itkPatchBasedDenoisingImageFilter.h Modules/Filtering/DisplacementField/include/itkBSplineExponentialDiffeomorphicTransform.hxx Modules/Filtering/DisplacementField/include/itkBSplineSmoothingOnUpdateDisplacementFieldTransform.h Modules/Filtering/DisplacementField/include/itkBSplineSmoothingOnUpdateDisplacementFieldTransform.hxx Modules/Filtering/DisplacementField/include/itkComposeDisplacementFieldsImageFilter.h Modules/Filtering/DisplacementField/include/itkConstantVelocityFieldTransform.h Modules/Filtering/DisplacementField/include/itkConstantVelocityFieldTransform.hxx Modules/Filtering/DisplacementField/include/itkDisplacementFieldTransform.h Modules/Filtering/DisplacementField/include/itkGaussianExponentialDiffeomorphicTransform.hxx Modules/Filtering/DisplacementField/include/itkGaussianSmoothingOnUpdateDisplacementFieldTransform.hxx Modules/Filtering/DisplacementField/include/itkGaussianSmoothingOnUpdateTimeVaryingVelocityFieldTransform.hxx Modules/Filtering/DisplacementField/include/itkInverseDisplacementFieldImageFilter.h Modules/Filtering/DisplacementField/include/itkInverseDisplacementFieldImageFilter.hxx Modules/Filtering/DisplacementField/include/itkInvertDisplacementFieldImageFilter.h Modules/Filtering/DisplacementField/include/itkLandmarkDisplacementFieldSource.h Modules/Filtering/DisplacementField/include/itkLandmarkDisplacementFieldSource.hxx Modules/Filtering/DisplacementField/include/itkTimeVaryingBSplineVelocityFieldTransform.h Modules/Filtering/DisplacementField/include/itkTimeVaryingBSplineVelocityFieldTransform.hxx Modules/Filtering/DisplacementField/include/itkTimeVaryingVelocityFieldIntegrationImageFilter.h Modules/Filtering/DisplacementField/include/itkTimeVaryingVelocityFieldTransform.hxx Modules/Filtering/DisplacementField/include/itkVelocityFieldTransform.h Modules/Filtering/DisplacementField/include/itkVelocityFieldTransform.hxx Modules/Filtering/DisplacementField/test/itkDisplacementFieldTransformCloneTest.cxx Modules/Filtering/FastMarching/include/itkFastMarchingBase.h Modules/Filtering/FastMarching/include/itkFastMarchingExtensionImageFilterBase.h Modules/Filtering/FastMarching/include/itkFastMarchingImageFilterBase.h Modules/Filtering/FastMarching/include/itkFastMarchingStoppingCriterionBase.h Modules/Filtering/ImageFeature/include/itkHoughTransform2DCirclesImageFilter.h Modules/Filtering/ImageFeature/include/itkHoughTransform2DLinesImageFilter.h Modules/Filtering/ImageFeature/test/itkHoughTransform2DCirclesImageTest.cxx Modules/Filtering/ImageFeature/test/itkHoughTransform2DLinesImageTest.cxx Modules/Filtering/ImageGrid/include/itkChangeInformationImageFilter.h Modules/Filtering/ImageGrid/include/itkExpandImageFilter.h Modules/Filtering/ImageGrid/include/itkInterpolateImageFilter.h Modules/Filtering/ImageGrid/include/itkResampleImageFilter.h Modules/Filtering/ImageGrid/include/itkSliceBySliceImageFilter.h Modules/Filtering/ImageGrid/include/itkVectorResampleImageFilter.h Modules/Filtering/ImageGrid/include/itkWarpImageFilter.h Modules/Filtering/ImageGrid/include/itkWarpVectorImageFilter.h Modules/Filtering/ImageGrid/test/itkChangeInformationImageFilterTest.cxx Modules/Filtering/ImageGrid/test/itkExpandImageFilterTest.cxx Modules/Filtering/ImageGrid/test/itkSliceBySliceImageFilterTest.cxx Modules/Filtering/ImageGrid/test/itkWarpImageFilterTest.cxx Modules/Filtering/ImageGrid/test/itkWarpVectorImageFilterTest.cxx Modules/Filtering/ImageIntensity/include/itkHistogramMatchingImageFilter.h Modules/Filtering/ImageIntensity/include/itkVectorExpandImageFilter.h Modules/Filtering/ImageIntensity/test/itkVectorExpandImageFilterTest.cxx Modules/Filtering/LabelMap/include/itkObjectByObjectLabelMapFilter.h Modules/Filtering/Path/include/itkPolyLineParametricPath.h Modules/Filtering/QuadEdgeMeshFiltering/include/itkParameterizationQuadEdgeMeshFilter.h Modules/Filtering/Thresholding/include/itkHistogramThresholdImageFilter.h Modules/IO/CSV/include/itkCSVArray2DFileReader.h Modules/IO/CSV/include/itkCSVArray2DFileReader.hxx Modules/IO/ImageBase/include/itkImageFileReader.h Modules/IO/ImageBase/include/itkImageFileWriter.h Modules/IO/ImageBase/include/itkImageSeriesReader.h Modules/IO/ImageBase/include/itkImageSeriesWriter.h Modules/IO/ImageBase/test/itkVectorImageReadWriteTest.cxx Modules/IO/Mesh/include/itkMeshFileReader.h Modules/IO/Mesh/include/itkMeshFileWriter.h Modules/IO/Mesh/test/itkMeshFileTestHelper.h Modules/IO/XML/include/itkDOMNodeXMLReader.h Modules/IO/XML/include/itkDOMNodeXMLWriter.h Modules/IO/XML/include/itkDOMReader.h Modules/IO/XML/include/itkDOMReader.hxx Modules/IO/XML/include/itkDOMWriter.h Modules/IO/XML/include/itkDOMWriter.hxx Modules/IO/XML/src/itkDOMNodeXMLReader.cxx Modules/IO/XML/test/itkDOMTest2.cxx Modules/Nonunit/Review/include/itkMultiScaleHessianBasedMeasureImageFilter.h Modules/Nonunit/Review/include/itkTransformToDisplacementFieldSource.h Modules/Numerics/FEM/include/itkFEMObject.h Modules/Numerics/FEM/include/itkFEMRobustSolver.hxx Modules/Numerics/FEM/include/itkFEMScatteredDataPointSetToImageFilter.h Modules/Numerics/FEM/include/itkFEMScatteredDataPointSetToImageFilter.hxx Modules/Numerics/FEM/include/itkFEMSolver.hxx Modules/Numerics/FEM/include/itkImageToRectilinearFEMObjectFilter.hxx Modules/Numerics/FEM/test/itkFEMRobustSolverTest.cxx Modules/Numerics/NeuralNetworks/include/itkBackPropagationLayer.hxx Modules/Numerics/NeuralNetworks/include/itkErrorBackPropagationLearningWithMomentum.hxx Modules/Numerics/NeuralNetworks/include/itkLayerBase.h Modules/Numerics/NeuralNetworks/include/itkMultilayerNeuralNetworkBase.hxx Modules/Numerics/NeuralNetworks/include/itkQuickPropLearningRule.hxx Modules/Numerics/NeuralNetworks/include/itkRBFBackPropagationLearningFunction.hxx Modules/Numerics/NeuralNetworks/include/itkRBFLayer.h Modules/Numerics/NeuralNetworks/include/itkRBFLayer.hxx Modules/Numerics/Optimizers/include/itkSingleValuedNonLinearOptimizer.h Modules/Numerics/Optimizersv4/include/itkMultiStartOptimizerv4.h Modules/Numerics/Optimizersv4/include/itkObjectToObjectMetric.h Modules/Numerics/Optimizersv4/include/itkObjectToObjectMetric.hxx Modules/Numerics/Optimizersv4/include/itkObjectToObjectOptimizerBase.h Modules/Numerics/Optimizersv4/src/itkGradientDescentOptimizerv4.cxx Modules/Numerics/Optimizersv4/src/itkMultiGradientOptimizerv4.cxx Modules/Numerics/Statistics/include/itkDistanceToCentroidMembershipFunction.h Modules/Numerics/Statistics/include/itkScalarImageToCooccurrenceMatrixFilter.h Modules/Numerics/Statistics/include/itkScalarImageToRunLengthMatrixFilter.h Modules/Numerics/Statistics/include/itkVectorContainerToListSampleAdaptor.h Modules/Registration/Common/include/itkCenteredTransformInitializer.h Modules/Registration/Common/include/itkCenteredVersorTransformInitializer.hxx Modules/Registration/Common/include/itkCompareHistogramImageToImageMetric.h Modules/Registration/Common/include/itkImageRegistrationMethod.h Modules/Registration/Common/include/itkImageToImageMetric.h Modules/Registration/Common/include/itkImageToSpatialObjectMetric.h Modules/Registration/Common/include/itkImageToSpatialObjectRegistrationMethod.h Modules/Registration/Common/include/itkMultiResolutionImageRegistrationMethod.h Modules/Registration/Common/include/itkMutualInformationImageToImageMetric.h Modules/Registration/Common/include/itkPointSetToImageMetric.h Modules/Registration/Common/include/itkPointSetToImageRegistrationMethod.h Modules/Registration/Common/include/itkPointSetToPointSetMetric.h Modules/Registration/Common/include/itkPointSetToPointSetRegistrationMethod.h Modules/Registration/Common/include/itkPointsLocator.h Modules/Registration/Common/include/itkSimpleMultiResolutionImageRegistrationUI.h Modules/Registration/Common/test/itkMutualInformationMetricTest.cxx Modules/Registration/FEM/include/itkFEMRegistrationFilter.h Modules/Registration/FEM/include/itkFEMRegistrationFilter.hxx Modules/Registration/FEM/include/itkPhysicsBasedNonRigidRegistrationMethod.hxx Modules/Registration/Metricsv4/include/itkImageToImageMetricv4.h Modules/Registration/Metricsv4/include/itkJointHistogramMutualInformationImageToImageMetricv4.h Modules/Registration/Metricsv4/include/itkLabeledPointSetToPointSetMetricv4.h Modules/Registration/Metricsv4/include/itkManifoldParzenWindowsPointSetFunction.h Modules/Registration/Metricsv4/include/itkManifoldParzenWindowsPointSetFunction.hxx Modules/Registration/Metricsv4/include/itkObjectToObjectMultiMetricv4.h Modules/Registration/Metricsv4/include/itkObjectToObjectMultiMetricv4.hxx Modules/Registration/Metricsv4/include/itkPointSetToPointSetMetricv4.h Modules/Registration/Metricsv4/include/itkPointSetToPointSetMetricv4.hxx Modules/Registration/PDEDeformable/include/itkMultiResolutionPDEDeformableRegistration.h Modules/Registration/PDEDeformable/test/itkMultiResolutionPDEDeformableRegistrationTest.cxx Modules/Registration/RegistrationMethodsv4/include/itkImageRegistrationMethodv4.h Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingBSplineVelocityFieldImageRegistrationMethod.hxx Modules/Registration/RegistrationMethodsv4/include/itkTimeVaryingVelocityFieldImageRegistrationMethodv4.hxx Modules/Registration/RegistrationMethodsv4/test/itkBSplineExponentialImageRegistrationTest.cxx Modules/Registration/RegistrationMethodsv4/test/itkExponentialImageRegistrationTest.cxx Modules/Registration/RegistrationMethodsv4/test/itkSimpleImageRegistrationTest.cxx Modules/Registration/RegistrationMethodsv4/test/itkSimpleImageRegistrationTest2.cxx Modules/Segmentation/BioCell/include/itkBioCellularAggregate.h Modules/Segmentation/Classifiers/include/itkBayesianClassifierInitializationImageFilter.h Modules/Segmentation/Classifiers/include/itkImageClassifierBase.h Modules/Segmentation/Classifiers/include/itkImageGaussianModelEstimator.h Modules/Segmentation/Classifiers/include/itkImageGaussianModelEstimator.hxx Modules/Segmentation/Classifiers/include/itkImageModelEstimatorBase.h Modules/Segmentation/DeformableMesh/include/itkDeformableSimplexMesh3DFilter.h Modules/Segmentation/LevelSets/include/itkExtensionVelocitiesImageFilter.hxx Modules/Segmentation/LevelSets/include/itkLevelSetNeighborhoodExtractor.h Modules/Segmentation/LevelSets/include/itkLevelSetVelocityNeighborhoodExtractor.h Modules/Segmentation/LevelSets/include/itkShapePriorMAPCostFunctionBase.h Modules/Segmentation/LevelSets/include/itkShapePriorSegmentationLevelSetImageFilter.h Modules/Segmentation/LevelSetsv4/include/itkBinaryImageToLevelSetImageAdaptor.h Modules/Segmentation/LevelSetsv4/include/itkBinaryImageToLevelSetImageAdaptorBase.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetContainerBase.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetDenseImage.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetDomainPartitionImage.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationAdvectionTerm.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationChanAndVeseExternalTerm.hxx Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationContainer.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationContainer.hxx Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationCurvatureTerm.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationPropagationTerm.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationTermBase.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationTermContainer.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetEquationTermContainer.hxx Modules/Segmentation/LevelSetsv4/include/itkLevelSetEvolution.hxx Modules/Segmentation/LevelSetsv4/include/itkLevelSetEvolutionBase.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetEvolutionComputeIterationThreader.hxx Modules/Segmentation/LevelSetsv4/include/itkLevelSetEvolutionStoppingCriterion.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetEvolutionUpdateLevelSetsThreader.hxx Modules/Segmentation/LevelSetsv4/include/itkLevelSetQuadEdgeMesh.h Modules/Segmentation/LevelSetsv4/include/itkLevelSetSparseImage.h Modules/Segmentation/LevelSetsv4/include/itkUpdateMalcolmSparseLevelSet.h Modules/Segmentation/LevelSetsv4/include/itkUpdateMalcolmSparseLevelSet.hxx Modules/Segmentation/LevelSetsv4/include/itkUpdateShiSparseLevelSet.h Modules/Segmentation/LevelSetsv4/include/itkUpdateShiSparseLevelSet.hxx Modules/Segmentation/LevelSetsv4/include/itkUpdateWhitakerSparseLevelSet.h Modules/Segmentation/LevelSetsv4/include/itkUpdateWhitakerSparseLevelSet.hxx Modules/Segmentation/LevelSetsv4/test/itkBinaryImageToMalcolmSparseLevelSetAdaptorTest.cxx Modules/Segmentation/LevelSetsv4/test/itkBinaryImageToShiSparseLevelSetAdaptorTest.cxx Modules/Segmentation/LevelSetsv4/test/itkBinaryImageToWhitakerSparseLevelSetAdaptorTest.cxx Modules/Segmentation/LevelSetsv4/test/itkLevelSetEquationChanAndVeseExternalTermTest.cxx Modules/Segmentation/LevelSetsv4/test/itkLevelSetEquationChanAndVeseInternalTermTest.cxx Modules/Segmentation/LevelSetsv4/test/itkLevelSetEquationCurvatureTermTest.cxx Modules/Segmentation/LevelSetsv4/test/itkLevelSetEquationLaplacianTermTest.cxx Modules/Segmentation/LevelSetsv4/test/itkLevelSetEquationPropagationTermTest.cxx Modules/Segmentation/LevelSetsv4/test/itkLevelSetEquationTermContainerTest.cxx Modules/Segmentation/LevelSetsv4/test/itkSingleLevelSetMalcolmImage2DTest.cxx Modules/Segmentation/LevelSetsv4/test/itkSingleLevelSetShiImage2DTest.cxx Modules/Segmentation/LevelSetsv4/test/itkSingleLevelSetWhitakerImage2DTest.cxx Modules/Segmentation/LevelSetsv4/test/itkSingleLevelSetWhitakerImage2DWithCurvatureTest.cxx Modules/Segmentation/LevelSetsv4/test/itkSingleLevelSetWhitakerImage2DWithLaplacianTest.cxx Modules/Segmentation/LevelSetsv4/test/itkSingleLevelSetWhitakerImage2DWithPropagationTest.cxx Modules/Segmentation/LevelSetsv4/test/itkTwoLevelSetMalcolmImage2DTest.cxx Modules/Segmentation/LevelSetsv4/test/itkTwoLevelSetShiImage2DTest.cxx Modules/Segmentation/LevelSetsv4/test/itkTwoLevelSetWhitakerImage2DTest.cxx Modules/Segmentation/LevelSetsv4Visualization/include/itkLevelSetIterationUpdateCommand.h Modules/Segmentation/SignedDistanceFunction/include/itkPCAShapeSignedDistanceFunction.h Modules/Video/Filtering/include/itkImageFilterToVideoFilterWrapper.h Utilities/Doxygen/doxygen.config.in Wrapping/Generators/Doc/doxygen.config.in