Command: Execute with const or non-const object


i could not figure out when an itk::Event invokes

virtual void Execute (Object *caller, const EventObject &event)


virtual void Execute (const Object *caller, const EventObject &event)

In both cases the invokation is like this->InvokeEvent( SomeEvent() ); It is based on the calling class?

Additional question:
The first method can call the second by calling it. To call the first method from the second one needs a const_cast.

Execute(const_cast<itk::Object*>(caller), event);

Is this advisable or are there safer methods?

Hi @Gordian,

The use of the two methods is based on the const state of the caller, i.e. the method invoked for

this->InvokeEvent( SomeEvent() );

depends on whether this is const or not.

It may be desirable to have a different implementation for the const version or only implement the const version. In other cases, re-using the method with a const_cast is fine.


1 Like

Hi @matt.mccormick,

thank you for your clarification.
As addition an answer from StackOverflow: Type of ‘this’ pointer.

1 Like