S
simudream
//hi maybe helpful others can look at this code and
//tell me why the class code won't behave like
//intrinsic types with post and pre increment
//Version: 1.00
#include <iostream>
using std::cout;
using std::endl;
using std:stream;
class IntWrapper
{
friend ostream & operator<<( ostream&, const IntWrapper& );
public:
IntWrapper( int val = 0 ) : i(val) {;}
IntWrapper operator+( const IntWrapper& rhs) const
{
//use RVO
return IntWrapper( this->i + rhs.i );
}
IntWrapper& operator++()
{
++this->i;
return *this;
}
IntWrapper operator++(int)
{
IntWrapper tmp(*this);
this->operator++();
return tmp;
}
private:
int i;
};
ostream &operator<<( ostream &, const IntWrapper & );
int main()
{
//interesting results of the post and pre increment operators
IntWrapper test;
int i = 0;
IntWrapper test1 = test + test++;
int test2 = i + i++;
//test1 should be the same as test2 but they are not
cout << test1 << " <- This should be the same as this one -> " <<
test2 << endl;
cout << i << i++ << test << test++ << endl;
system("pause");
}
ostream &operator<<( ostream &output, const IntWrapper &r )
{
output << r.i;
return output;
}
//tell me why the class code won't behave like
//intrinsic types with post and pre increment
//Version: 1.00
#include <iostream>
using std::cout;
using std::endl;
using std:stream;
class IntWrapper
{
friend ostream & operator<<( ostream&, const IntWrapper& );
public:
IntWrapper( int val = 0 ) : i(val) {;}
IntWrapper operator+( const IntWrapper& rhs) const
{
//use RVO
return IntWrapper( this->i + rhs.i );
}
IntWrapper& operator++()
{
++this->i;
return *this;
}
IntWrapper operator++(int)
{
IntWrapper tmp(*this);
this->operator++();
return tmp;
}
private:
int i;
};
ostream &operator<<( ostream &, const IntWrapper & );
int main()
{
//interesting results of the post and pre increment operators
IntWrapper test;
int i = 0;
IntWrapper test1 = test + test++;
int test2 = i + i++;
//test1 should be the same as test2 but they are not
cout << test1 << " <- This should be the same as this one -> " <<
test2 << endl;
cout << i << i++ << test << test++ << endl;
system("pause");
}
ostream &operator<<( ostream &output, const IntWrapper &r )
{
output << r.i;
return output;
}