C++ 什么';这[i]在c++;无过载

C++ 什么';这[i]在c++;无过载,c++,C++,这是WebKit中的代码: class ExecState : public Register { JSValue calleeAsValue() const { return this[JSStack::Callee].jsValue(); } ... } JSStack::被调用方是const,运算符[]在ExecState或寄存器中没有重载 < C++ > > [jsStay::Callee ] < /p> > < p>,>代码> < 是指向 ExcSturt的指针,使用指

这是WebKit中的代码:

class ExecState : public Register 
{
 JSValue calleeAsValue() const { return this[JSStack::Callee].jsValue(); } 
 ... 
}
JSStack::被调用方
const
运算符[]
ExecState
寄存器中没有重载


< C++ > <代码> > [jsStay::Callee ] < /p> >

< p>,>代码> < <代码>是指向<代码> ExcSturt<代码>的指针,使用指针的下标运算符使其表现为它是数组。也就是说,表达式
this[JSStack::Callee]
访问的对象是
JSStack::Callee
元素远离
this
。当然,只有当元素是
ExecState
对象数组的成员时,这才可能起作用

下面是使用此“功能”的快速独立演示。一般来说,我建议不要使用它,但是如果已知在数组中使用了一个类型,并且访问是可行的,那么可能会有非常特殊的需求。例如,如果该类型是在本地定义的,那么所有已知的用法都可能是已知的(不过,我会添加一条注释说明该假设)

#包括
福班{
int d_值;
公众:
foo(inti):d_值(i){
int get(int i)const{返回这个[i].d_值;}
};
模板
int size(T(&)[size]){return size;}
int main()
{
foo f[]={9,1,8,2,7,3,6,4,5};
对于(int i=0;istd::难道你没有试着从代码中删除它,看看会发生什么吗?嗯,这不是一件好事。如果
JSStack::Callee
的计算结果不为0,那么你就遇到了大问题。如果它为0,那么你真的还存在问题。不要在家里尝试这个方法?我想如果你知道
*这个
是相同数组的一个元素,它可能会起作用类型(而不是子类型!)。但这是一个丑陋的黑客。@MatstPeterson不,
这是一个指针类型。您描述的行为将通过
(*this)[/*…*/]
实现。
#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';
    }
}