今天网上翻资料发现居然有:
编译运行,输出5。 厉害了我的哥,不过这能用在什么地方呢
object[ { arg1, arg2, ... } ]这种写法(在下标表达式中使用用户定义的 operator[] ,其中列表初始化初始化重载运算符的参数)。于是决定自己写代码试试。
struct Foo
{
int x[10];
int& operator [](initializer_list<int> l)
{
int sub = 0;
for (auto& v : l)
sub += v;
sub %= 10;
return x[sub];
}
};
int main()
{
Foo f = { { 1, 2, 3, 4, 5, 6, 7, 8} };
cout << f[{1, 5, -2}] << endl;
}
编译运行,输出5。 厉害了我的哥,不过这能用在什么地方呢