64,648
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
class A
{
public:
A(int i = 0) : num(i) {};
A& operator = (const A& source)
{
this->num = source.num;
return(*this);
}
operator int()
{
return num;
}
private:
int num;
};
int main()
{
A i, j(1), k(2);
if ((j=k) = i)
std::cout<<"OK"<<std::endl;
return(0);
}
#include <iostream>
using namespace std;
class A
{
public:
A():data_(0){}
~A(){}
template< class T>
A( const T& other )
{
operator = (other );
}
template< class T>
A& operator = ( const T& other )
{
copy( other );
return *this;
}
A& operator +=( const A& other )
{
data_ += other.data_;
return *this;
}
operator bool()
{
return data_ ? true : false;
}
friend const A operator +( const A& lhs, const A& rhs )
{
A tmp( lhs );
tmp += rhs;
return tmp;
}
//attention, return a reference
friend ostream& operator << ( ostream& os, const A& rhs )
{
return rhs.print( os );
}
private:
int data_;
void copy( const A& other )
{
data_ = other.data_;
}
void copy( const int& other )
{
data_ = other;
}
ostream& print( ostream& os ) const
{
return os << data_;
}
};
int main()
{
A s;
A t;
s = 5;
t = 10;
cout << s << endl;
s = s + t;
cout << s << endl;
s += t;
cout << s << endl;
s += 7;
cout << s << endl;
if ( t = ( s = 18 ) = 19 )
cout << t << endl;
return 0;
}