gpt4 book ai didi

c++ - 令人困惑的 (*this) 指针转换

转载 作者:太空狗 更新时间:2023-10-29 20:14:46 24 4
gpt4 key购买 nike

这是 Transform 类中 ROS ( link) 中 Transform.h 的代码。

/**@brief Return the transform of the vector */
TFSIMD_FORCE_INLINE Vector3 operator*(const Vector3& x) const
{
return (*this)(x);
}

谁能解释一下这段代码在做什么?这是我的想法(对于上下文,我有几年的 C 程序员经验,第一次用 C++ 开发。)

调用以下函数时调用该函数

object_of_type_Transform * object_of_type_Vector3

然后它将 Vector3 对象转换为 Transform 对象并将其作为结果返回(我很不清楚这怎么可能,因为这两种类型似乎不兼容)。

但是返回的结果是一个 Vector3...这就是我的心智模型崩溃的地方。

此外,该函数应该是基于 Transform 类转换 Vector3 点...所以我的理解肯定在某处存在缺陷。

如果有任何见解,我将不胜感激。

谢谢

编辑

感谢回复!上面的函数是:

/**@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);
}

我明白了,现在,发生了什么事。再次感谢。

最佳答案

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

没有。没有类型转换正在发生;这个:

return (*this)(x);

相当于:

return this->operator()(x);

在这两种情况下,代码都会调用 Transform::operator() 并将 x 传递给它。第一个代码中的括号是必需的,因为 ()* 绑定(bind)更强,所以如果 *this 周围没有括号,代码将等同于 return *(this(x)); – 编译错误。

顺便说一句,这是非常地道的 C++ 代码,但我可以看出语法对于 C 程序员来说是多么的困惑(毕竟,你不能重载运算符,更不用说 operator() 了)。

关于c++ - 令人困惑的 (*this) 指针转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com