【发布时间】:2010-12-23 14:37:04
【问题描述】:
我目前开始研究 C++ 中的运算符重载,用于一个简单的 2D 顶点类,其中位置应该可以通过 [] 运算符获得。这通常有效,但我真的不知道如何处理错误,例如如果运算符超出范围(在只有 x 和 y 值的 2D 顶点类的情况下,如果它大于一)
在这种情况下处理错误的常用方法是什么?
谢谢
【问题讨论】:
-
这就是我现在所做的:class ArrayOutOfBounds: public exception{ virtual const char* what() const throw() { return "The Array you are out of bounds!"; } }; T& operator[](uint32 _i){ try{ if(_i>1){ throw exOutOfBounds; } 其他 { 返回位置 [_i]; } } catch (exception& e){ cout
标签: c++ error-handling operator-overloading