Confusing (*this) cast of a pointer

c++, casting, ros

Solution

It then casts the Vector3 object into a Transform object and returns that as a result

No. No cast is taking place; this:

return (*this)(x);

is equivalent to:

return this->operator()(x);

In both cases the code is invoking `Transform::operator()` and passing `x` to it. The parentheses in the first code are necessary because `()` binds stronger than `*` so without the parentheses around `*this` the code would be equivalent to `return *(this(x));` – a compilation error.

Incidentally, this is quite idiomatic C++ code but I can see how the syntax can be confusing for C programmers (where, after all, you cannot overload operators, let alone `operator()`).

Problem

This is code from Transform.h in ROS (link) in the Transform class. ``` /**@brief Return the transform of the vector */ TFSIMD_FORCE_INLINE Vector3 operator*(const Vector3& x) const { return (*this)(x); } ``` Can someone explain what this piece of code is doing? Here is what I think (for context, I have a few years experience as a C programmer, first time developing in C++.) The function is called when the following is invoked ``` object_of_type_Transform * object_of_type_Vector3 ``` It then casts the Vector3 object into a Transform object and returns that as a result (I am quite unclear about how this can be possible as the two types do not seem compatible). But the result being returned is a Vector3... and this is where my mental model breaks down. Additionally, the function is supposed to be transforming a Vector3 point based on the Transform class... so my understanding is definitely flawed somewhere. I would appreciate any insights. Thanks EDIT Thanks for the reply! The function right above is: ``` /**@brief Return the transform of the vector */ TFSIMD_FORCE_INLINE Vector3 operator()(const Vector3& x) const { return Vector3(m_basis[0].dot(x) + m_origin.x(), m_basis[1].dot(x) + m_origin.y(), m_basis[2].dot(x) + m_origin.z()); } /**@brief Return the transform of the vector */ TFSIMD_FORCE_INLINE Vector3 operator*(const Vector3& x) const { return (*this)(x); } ``` I understand, now, whats going on. Thanks again.

Original source