As chris suggested, I would say that you should just use tm , not your own date class:
tm a{0, 0, 0, 15, 5, 2006 - 1900}; cout << put_time(&a, "%Y-hello-%d-world-%m-something-%d%d");
If you must implement custom functions that cannot be executed with get_time and put_time , then you probably want to use the tm member as part of your class so that you can simply extend the functionality that already exists:
class CDate{ tm m_date; public: CDate(int year, int month, int day): m_date{0, 0, 0, day, month, year - 1900}{} const tm& getDate() const{return m_date;} }; ostream& operator<<(ostream& lhs, const CDate& rhs){ auto date = rhs.getDate(); return lhs << put_time(&a, "%Y-hello-%d-world-%m-something-%d%d"); }
Then you can use CDate as follows:
CDate a(2006, 5, 15); cout << "DATE IS:" << a;
EDIT:
If you look at your question again, I think that you have a misconception about how the insert operator works, you cannot pass both the object and the format: https://msdn.microsoft.com/en-us/ library / 1z2f6c2k.aspx
If you want to specify the format, but save your CDate class, I would again suggest using put_time :
cout << put_time(&a.getDate(), "%Y-hello-%d-world-%m-something-%d%d");
If you insist again on writing your own format accept function, you need to create a helper class that can be built in-line and supported by the insert statement:
class put_CDate{ const CDate* m_pCDate; const char* m_szFormat; public: put_CDate(const CDate* pCDate, const char* szFormat) : m_pCDate(pCDate), m_szFormat(szFormat) {} const CDate* getPCDate() const { return m_pCDate; } const char* getSZFormat() const { return m_szFormat; } }; ostream& operator<<(ostream& lhs, const put_CDate& rhs){ return lhs << put_time(&rhs.getPCDate()->getDate(), rhs.getSZFormat()); }
You can use it as follows:
cout << put_CDate(&a, "%Y-hello-%d-world-%m-something-%d%d") << endl;