at() 和重载运算符 [ ] C++ 之间的区别

Difference between at() and overloading operator [ ] C++

at()和重载operator[ ]有什么区别?除了 at() 提供边界检查并抛出异常外,它们的实现有什么区别 out_of_range?

实现operator[ ]:

 const int LIMIT =100;
 .......................
 int& operator[ ] (int n) const

 {
    if(n<0 || n >= LIMIT)
    {
      std::cout<<"Error index!"<<std::endl;
      exit(1);
    }
    return arr[ n ];
 }

能否给个实现思路at()

Herb Sutter 的书 exceptional c++ style 在第 1 项中涵盖了这个精确的主题:at 应用边界检查并将抛出异常,其中 [] 将在越界使用时执行未定义的行为。我们可以自由选择我们想要使用哪一个,这符合 c++ 哲学,即一个人应该只为一个人使用什么付费。 at() 可能会更昂贵,因为它会执行检查。

http://www.cplusplus.com/reference/vector/vector/at/

你可以看一下类似 gcc 的实现。