关于C++primer的疑问

来源:互联网 发布:网络十大博客公司 编辑:程序博客网 时间:2024/05/18 13:45

看c++已有半个月了,看到一段代码

checkedptr& checkedptr::operator++(int){checkedptr ret(*this);++*this;return ret;}


就用vs2010实现了一下,代码如下

#include<iostream>using namespace std;class checkedptr{public:checkedptr(int *b,int *e):beg(b),end(e),curr(b){}checkedptr& operator++();checkedptr& operator++(int);friend ostream& operator<<(ostream& out,const checkedptr &c);~checkedptr(){}private:int* beg;int* end;int* curr;};checkedptr& checkedptr::operator++(){if(curr==beg)throw out_of_range("increment past the end of checkedptr");++curr;return *this;}checkedptr& checkedptr::operator++(int){checkedptr ret(*this);++*this;return ret;}ostream& operator<<(ostream& out,const checkedptr &c){out<<*(c.curr);return out;}int main(){int arr[]={1,2,3,4,5,6,7,8,9};checkedptr parr(arr,arr+9);cout<<"++a:"<<++parr;checkedptr par(arr+4,arr+9);cout<<"a++:"<<++par;return 0;}


结果编译没问题,运行时出错。

求大神指导
 

原创粉丝点击