夏天/isummer

Sun of my life !Talk is cheap, Show me the code! 追风赶月莫停留,平芜尽处是春山~

博客园 首页 新随笔 联系 管理

1. 数组运算符重载,以及异常抛出

(1)一个是const类型重载,只返回元素值,不能修改

(2)一个是非const类型重载,返回元素值,并且可直接操作修改

分别应用于const vector2对象,或vector2对象

            double  operator[] (int i) const
            {
                if((i < 0)||(i > 1))
                    throw std::runtime_error("Out of bounds in Vector2::operator[]") ;

                return _xyz[i];
            }

            double& operator[] (int i)
            {
                if((i < 0)||(i > 1))
                    throw std::runtime_error("Out of bounds in Vector2::operator[]") ;

                return _xyz[i];
            }

 

 

2.

3.

 

4.

5.

 

endl;

 

posted on 2020-08-22 14:09  夏天/isummer  阅读(226)  评论(0)    收藏  举报