C++ - definition of (*this)
C++ - definition of (*this)
我有一个用c++编写的代码,我不知道这个代码应该做什么。我试过在论坛上搜索,但我仍然很困惑。有人能帮我定义一下这个代码吗?
inline void normalize() {
const float inv_length = 1.0f / get_length();
(*this) *= inv_length;
}
(*this)语法真的让我很困惑,它指的是返回值吗?如果你有更多的时间,你能用Java重写它吗?
this
是指向调用成员函数的当前对象的指针。
*this
只是取消引用那个指针,所以它是对那个对象的引用。
重写为Java:
void normalize() {
final float inv_length = 1.0f / get_length();
this.multiply(inv_length); // Since there is no operator overloading in Java, this had to be converted to a method.
// Note that the "this." above is optional.
}
虽然this
关键字在C++中为您提供了一个指针(因为引用是在this
之后添加到C++的),但正如Nicky C.所指出的那样,相同的关键字在Java中为您带来了一个引用
(this)是一个指针,即存储变量的地址。它前面的星号(*)表示这是一个指针。
(*this) *= inv_length;
与相同
(*this) = (*this) * inv_length;
(*this)是指向的任何(this)的值
您的语句所做的是将存储在指针中的值乘以inv_length。
相关文章:
- 表示"accepting anything for this template argument" C++概念的通配符
- 为什么使用 "this" 指针调用派生成员函数?
- Usages of std::move
- C++错误:"error: int aaa::bbb is protected within this context"
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 如何处理 c++ 中类实现中的"invalid use of non-static data member"?
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 具有变量Number of Arguments的std::函数的矢量
- Capacity of a deque
- C++ "Invalid use of 'this' in non-member function" ,
- Windows 10 环境中的"This version of XAudio2 is available only in Windows 8"错误
- C++ substr 方法 - "invalid use of ‘this’ in non-member function"
- 科学实验室编译"cannot allocate this quantity of memory"
- std::shared_ptr of this
- C++ - definition of (*this)
- 如何抑制此内容 - qglobal.h:320:6: 警告: #warning "This version of Mac OS X is unsupported" ?
- VC++ Winforms自动生成的项目无法调试VS2010:"This Project is out of Date"
- "error: C2275: 'QMouseEvent' : illegal use of this type as an expression"
- 简单的模板化函数,用于转换 std::vectors - "illegal use of this type as an expression"