我想超載std::ostream& operator<<
這樣做:重載運營商的std :: ostream的和運營商<<打印實例內存地址
#include <iostream>
class MyTime
{
private:
unsigned char _hour;
unsigned char _minute;
float _second;
signed char _timeZone;
unsigned char _daylightSavings;
double _decimalHour;
friend std::ostream& operator<<(std::ostream&, const MyTime&);
public:
MyTime();
~MyTime();
};
和操作執行:
std::ostream& operator<<(std::ostream &strm, const MyTime &time)
{
return strm << (int)time._hour << ":" << (int)time._minute << ":" << time._second << " +" << (int)time._daylightSavings << " zone: " << (int)time._timeZone << " decimal hour: " << time._decimalHour;
}
但是,當我這樣做:
MyTime* anotherTime = new MyTime(6, 31, 27, 0, 0);
std::cout << "Time: " << anotherTime << std::endl;
它只打印anotherTime
內存地址。
我在做什麼錯?
或者可能'指明MyTime對應anotherTime(6,31,27,0,0);' – juanchopanza
@juanchopanza是的,絕對。 –
是的,你是對的。我剛開始學習C++。謝謝。 – VansFannel