64,640
社区成员
发帖
与我相关
我的任务
分享
ct operator++();
ct ct::operator++()
{
++itsval; //主要
ct temp; //主要
temp.setitsval(itsval); //主要
return temp; //主要
}
#include <iostream>
using namespace std;
class Iter
{
public:
Iter(int *q):p(q)
{
}
Iter(const Iter &obj)
{
*this=obj;
}
Iter& operator =(const Iter &obj)
{
p=const_cast<int*>(obj.p);
return *this;
}
int& operator *() // 同时实现非const
{
return *p;
}
const int& operator *() const //与const版的所有函数,那样比较好
{
return *p;
}
Iter& operator ++()
{
p++;
return *this;
}
const Iter operator ++(int)
{
Iter temp(*this);
++(*this);
return temp;
}
private:
int *p;
};
int main()
{
int arr[2]={1,2};
Iter a(arr);
Iter b(arr);
if( *(a++) == (*b) )
{
cout<<"相等"<<endl;
}
return 0;
}
ct& ct::operator++()
{
++itsval;
return *this;
}
ct ct::operator++(int)
{
ct temp(*this);
++itsval;
return temp;
}
ct &operator++();//前置++
constct operator++(int);//后置++
ct &ct::operator++()
{
++itsval;
return *this;
}
const ct ct::operator++(int)
{
ct temp = *this;
++*this;
return temp;
}