现在的位置: 首页 > 综合 > 正文

c++ <<运算符重载

2012年04月19日 ⁄ 综合 ⁄ 共 397字 ⁄ 字号 评论关闭
// overload_date.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

class Date
{
    int mo, da, yr;
public:
    Date(int m, int d, int y)
    {
        mo = m; da = d; yr = y;
    }
    friend ostream& operator<<(ostream& os, const Date& dt);
};

ostream& operator<<(ostream& os, const Date& dt)
{
    os << dt.mo << '/' << dt.da << '/' << dt.yr;
    return os;
}

int main()
{
    Date dt(5, 6, 92);
    cout << dt;
}

来自:http://msdn.microsoft.com/en-us/library/1z2f6c2k(v=VS.80).aspx

记在这里以后就不用去其他地方找了。

抱歉!评论已关闭.