这[i]在C ++中的含义是什么,没有重载
what's the meaning of this[i] in c++ without overloading
这是WebKit的代码:
class ExecState : public Register
{
JSValue calleeAsValue() const { return this[JSStack::Callee].jsValue(); }
...
}
JSStack::Callee
为const
,Operator[]
在ExecState
或Register
中未过载,
那么this[JSStack::Callee]
的c++中的语法是什么呢?
好吧,this
是指向ExecState
的指针,将下标运算符与指针一起使用会使其表现得像一个数组。也就是说,表达式this[JSStack::Callee]
访问与this
相距JSStack::Callee
个元素的对象。当然,只有当元素是ExecState
对象数组的成员时,这才可能起作用。
下面是使用此"功能"的快速独立演示。一般来说,我建议不要使用它,但可能存在非常特殊的需求,因为已知在数组中使用了类型,并且访问是可行的。例如,如果类型是在本地定义的,那么所有已知的用途都是已知的(不过,我会添加一条注释来说明这一假设)。
#include <iostream>
class foo {
int d_value;
public:
foo(int i): d_value(i) {}
int get(int i) const { return this[i].d_value; }
};
template <typename T, int Size>
int size(T(&)[Size]) { return Size; }
int main()
{
foo f[] = { 9, 1, 8, 2, 7, 3, 6, 4, 5 };
for (int i=0; i < size(f) - 2; ++i) {
std::cout << "f[" << i << "].get(2)=" << f[i].get(2) << 'n';
}
}
相关文章:
- 重载运算符的范围是什么?它是否会影响作为类成员的集合的插入函数?
- 函数模板实例化、替换和重载解析的顺序是什么?
- "operator()"在重载运算符方法中是什么意思,在priority_queue(STL)中用作C++中的比较器?
- C++,()运算符重载,它的工作是什么
- C++函数重载,具体步骤是什么
- 在 std::visit 中跳过变体类型的一些重载组合的方法是什么?
- c++运算符重载-我实际返回的操作数类型是什么
- 将显式指定的函数模板重载作为模板参数传递的正确语法是什么?
- 将强制转换运算符重载到 std::map 的正确语法是什么
- std::forward() 的右值引用重载的目的是什么?
- & 字符在重载 std::cout 中的功能是什么?
- 运算符重载中变量的数据类型和范围应该是什么
- 重载加号运算符的正确方法是什么
- C++重载运算符两次,一个返回非常量引用,另一个返回常量引用,首选项是什么
- 重载全常量类型的复制赋值运算符的正确方法是什么?
- 使用重载添加两个矩阵的最佳方法是什么?
- 这个算子重载函数重载是什么
- 这[i]在C ++中的含义是什么,没有重载
- c++重载的+运算符没有返回一个临时对象,原因是什么
- "&"和"*"在运算符重载中的作用是什么?