# Command: Execute with const or non-const object

**URL:** <https://discourse.itk.org/t/command-execute-with-const-or-non-const-object/1587>\
**Category:** Engineering\
**Tags:** command, observer, event\
**Created:** [February 10, 2019, 3:52pm UTC](https://discourse.itk.org/t/command-execute-with-const-or-non-const-object/1587 "2019-02-10T15:52:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gordian](https://discourse.itk.org/user_avatar/discourse.itk.org/gordian/32/335_2.png) [@Gordian](https://discourse.itk.org/u/Gordian)\
**Post date:** [February 10, 2019, 3:52pm UTC](https://discourse.itk.org/t/command-execute-with-const-or-non-const-object/1587/1 "2019-02-10T15:52:45Z")

</div>

Hello,

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

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

or

`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?

---

<div class="post-metadata">

**Author:** ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)\
**Post date:** [February 11, 2019, 4:01pm UTC](https://discourse.itk.org/t/command-execute-with-const-or-non-const-object/1587/2 "2019-02-11T16:01:09Z")

</div>

Hi @Gordian,

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

```auto
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.

HTH,  
Matt

---

<div class="post-metadata">

**Author:** ![Gordian](https://discourse.itk.org/user_avatar/discourse.itk.org/gordian/32/335_2.png) [@Gordian](https://discourse.itk.org/u/Gordian)\
**Post date:** [February 12, 2019, 12:10pm UTC](https://discourse.itk.org/t/command-execute-with-const-or-non-const-object/1587/3 "2019-02-12T12:10:27Z")

</div>

Hi @matt.mccormick,

thank you for your clarification.  
As addition an answer from StackOverflow: [Type of ‘this’ pointer](https://stackoverflow.com/a/6067267/3921660).
