#include <ostream> // 是不是太重量级了? class Date { public: Date(int year, int month, int day) : year_(year), month_(month), day_(day) { } void writeTo(std::ostream& os) const { os << year_ << '-' << month_ << '-' << day_; } private: int year_, month_, day_; }; std::ostream& operator<<(std::ostream& os, const Date& date) { date.writeTo(os); return os; } int main() { Date date(2011, 4, 3); std::cout << date << std::endl; // 输出 2011-4-3 }