Skip to content

Commit 1c86090

Browse files
committed
STYLE: Explicitly declare virtual for derived class member functions
Class member functions derived from parent class with virtual functions of the same name are implicitly virtual. This patch set uses a macro for the c++11 override keyword to indicate that this function was intended to be overridden in this derived class. Ensuring that functions signatures are described as virtual is not strictly needed, but it facilitates maintenance by being consistent. Approximately 1/2 of the toolkit had the explicit virtual designation and 1/2 relied on implicit behavior. The ITK_OVERRIDE define allows for non-c++11 compilers to continue to work. Change-Id: Iba5c2970f2bad4f7f664b92a4420d217546bb22f
1 parent 638f704 commit 1c86090

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Modules/Core/Common/include/itkMacro.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ namespace itk
118118
#endif
119119
#endif
120120

121+
// In c++11 the override keyword allows you to explicity define that a function
122+
// is intended to override the base-class version. This makes the code more
123+
// managable and fixes a set of common hard-to-find bugs.
124+
#if __cplusplus >= 201103L
125+
#define ITK_OVERRIDE override
126+
#else
127+
#define ITK_OVERRIDE
128+
#endif
129+
130+
121131
/** Define two object creation methods. The first method, New(),
122132
* creates an object from a class, potentially deferring to a factory.
123133
* The second method, CreateAnother(), creates an object from an
@@ -156,7 +166,7 @@ namespace itk
156166
}
157167

158168
#define itkCreateAnotherMacro(x) \
159-
virtual::itk::LightObject::Pointer CreateAnother(void) const \
169+
virtual::itk::LightObject::Pointer CreateAnother(void) const ITK_OVERRIDE \
160170
{ \
161171
::itk::LightObject::Pointer smartPtr; \
162172
smartPtr = x::New().GetPointer(); \
@@ -196,7 +206,7 @@ namespace itk
196206
rawPtr->UnRegister(); \
197207
return smartPtr; \
198208
} \
199-
virtual::itk::LightObject::Pointer CreateAnother(void) const \
209+
virtual::itk::LightObject::Pointer CreateAnother(void) const ITK_OVERRIDE \
200210
{ \
201211
::itk::LightObject::Pointer smartPtr; \
202212
smartPtr = x::New().GetPointer(); \

Modules/Core/Common/include/itkObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class ITKCommon_EXPORT Object:public LightObject
7171
* object that is exactly the same type as the referring object.
7272
* This is useful in cases where an object has been cast back to a
7373
* base class. */
74-
virtual LightObject::Pointer CreateAnother() const;
74+
virtual LightObject::Pointer CreateAnother() const ITK_OVERRIDE;
7575

7676
/** Standard part of all itk objects. */
7777
itkTypeMacro(Object, LightObject);

0 commit comments

Comments
 (0)