马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 析觇啊 于 2020-4-23 23:02 编辑 #include <iostream>
class MyInt
{
friend std::ostream& operator << (std::ostream& out, const MyInt &myint);
public:
MyInt()
{
m_num = 0;
}
MyInt& operator++ ();
MyInt operator++(int);
private:
int m_num;
};
MyInt& MyInt::operator++()
{
this->m_num += 1;
return *this;
}
MyInt MyInt::operator++(int)
{
MyInt temp = *this;
this->m_num++;
return temp;
}
std::ostream& operator << (std::ostream& out, const MyInt &myint)
{
out << myint.m_num;
return out;
}
int main()
{
MyInt myint;
std::cout << (++(++myint)) << std::endl;
std::cout << myint++ << std::endl;
//myint++;
//std::cout << myint << std::endl;
}
请问这段代码哪里有问题呢。我重载后置++后确实是返回MyInt类了呀。
而且为什么如果我按注释的46和47行就可以成功输出呢。谢谢大家!
你在 你的帖子下面回复我,我是没有通知的,除非我点进来才能知道你回复了我
#include <iostream>
class MyInt
{
friend std::ostream& operator << (std::ostream& out, const MyInt &myint);
public:
MyInt()
{
m_num = 0;
}
MyInt& operator++ ();
MyInt operator++(int);
private:
int m_num;
};
MyInt& MyInt::operator++()
{
this->m_num += 1;
return *this;
}
MyInt MyInt::operator++(int)
{
MyInt temp = *this;
this->m_num++;
return temp;
}
std::ostream& operator << (std::ostream& out, const MyInt &myint)
{
out << myint.m_num;
return out;
}
int main()
{
MyInt myint;
std::cout << (++(++myint)) << std::endl;
std::cout << myint++ << std::endl;
//myint++;
//std::cout << myint << std::endl;
}
|