gpt4 book ai didi

c++ - 运算符 [] 长版和短版

转载 作者:行者123 更新时间:2023-11-28 05:29:56 25 4
gpt4 key购买 nike

使用较长版本的 (something).operator[]() 而不是简单的 (something)[] 有什么好处?

例如:

std::array<int, 10> arr1;
std::array<int, 10> arr2;

for(int i = 0; i < arr1.size(); i++)
std::cout << arr1[i] << ' ';
std::cout << std::endl;

for(int i = 0; i < arr2.size(); i++)
std::cout << arr2.operator[](i) << ' ';
std::cout << std::endl;

最佳答案

没有。 [] 只是用户定义类型上 operator[] 的语法糖。当您自己定义这些函数时,您只需要 operator 语法。这适用于所有运算符,例如 operator()operator[]operator newoperator=、.. .

关于c++ - 运算符 [] 长版和短版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39773580/

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