Я хочу иметь возможность создать оператор присваивания для структуры C (struct tm
в <time.h>
/ std::tm
в <ctime>
), которую я использую в своем коде C ++.Это не нужно для моей программы, мне только интересно, можно ли переопределить.Любая помощь будет принята с благодарностью.
в заголовочном файле tm_operators.hpp:
#ifndef tm_operators_hpp
#define tm_operators_hpp
#include <ostream>
#include <ctime>
static inline bool operator== (const std::tm &dt1, const std::tm &dt2 ) {
return (dt1.tm_yday == dt2.tm_yday and dt1.tm_year == dt2.tm_year);
} /* Works fine */
static inline bool operator!= (const std::tm &dt1, const std::tm &dt2) {
return not operator== (dt1, dt2);
} /* Works fine */
static inline bool operator< (const std::tm &dt1, const std::tm &dt2) { ... } /* Works fine */
static inline bool operator> (const std::tm &dt1, const std::tm &dt2) {
return operator< (dt2, dt1);
} /* Works fine */
static inline bool operator<= (const std::tm &dt1, const std::tm &dt2) {
return not operator> (dt1, dt2);
} /* Works fine */
static inline bool operator>= (const std::tm &dt1, const std::tm &dt2) {
return not operator< (dt1, dt2);
} /* Works fine */
static inline std::ostream & operator<< (std::ostream &os, const std::tm &dt) {
char buff[11]; /* 11 characters in "dd/mm/YYYY\0" */
std::strftime (buff, 11, "%d/%m/%Y", &dt);
return os << buff;
} /* Works fine */
static inline std::tm & operator= (std::tm &dt, const char * str) {
strptime (str, "%d/%m/%Y", &dt);
return dt;
} /* This requires it to be part of the original struct which I can't modify */
#endif /* tm_operators_hpp */
Я получаю ошибку Overloaded 'operator=' must be a non-static member function
.У меня нет доступа к определению класса, так как я могу установить этот оператор?