看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;
}
结果编译没问题,运行时出错。
求大神指导