# 运算符重载里面程序运行细节

138人阅读 评论(0)

class Integer
{
public:
Integer(int num = 0);
~Integer();
Integer(const Integer& other);
Integer &operator=(int num);
Integer &operator=(Integer &other);
friend Integer operator+(int num, Integer &me);
Integer operator+(int num);
int GetNum();
private:
int _num;
};

Integer::Integer(int num) :_num(num)
{
std::cout << "Integer(int)" << _num  << std::endl;
}

Integer::~Integer()
{
std::cout << "~Integer()" << std::endl;
}

Integer::Integer(const Integer & other)
{
_num = other._num;
std::cout << "Integer(Integer&)" << _num << std::endl;
}

Integer & Integer::operator=(int num)
{
_num = num;
std::cout << "operator=(int)" << _num <<std::endl;
return *this;
}

Integer & Integer::operator=(Integer & other)
{
_num = other._num;
std::cout << "operator=(Integer&)" << _num << std::endl;
return *this;
}

Integer Integer::operator+(int num)
{
Integer temp = _num;
temp._num += num;
std::cout << "operator+(int)" << temp._num << std::endl;
return temp;
}

int Integer::GetNum()
{
return _num;
}

Integer operator+(int num, Integer & me)
{
Integer temp = me._num;
temp._num += num;
std::cout << "operator+(friend&)" << temp._num << std::endl;
return temp;
}

int main()
{
Integer demo = 10, other ;
other = 100 + demo + 200;
return 0;
}

int main()
{
Integer demo = 10;
Integer other = 100 + demo + 200;
return 0;
}

Integer other = 100 + demo + 100；

Integer& operator++();
Integer operator++(int);

Integer & Integer::operator++()
{
_num += 1;
return *this;
}

Integer Integer::operator++(int)
{
Integer temp = *this;
_num += 1;
return temp;
}

int n1 = 10;
int n2 = n1++;

friend std::ostream & operator<<(std::ostream & lhs, const Integer & rhs)
{
lhs << rhs._num;
return lhs;
}

friend std::istream& operator>>(std::istream &lhs, Integer &rhs)
{
lhs >> rhs._num;
return lhs;
}
0
0

个人资料
• 访问：8561次
• 积分：698
• 等级：
• 排名：千里之外
• 原创：64篇
• 转载：0篇
• 译文：0篇
• 评论：0条
文章分类