Здравствуйте, cs_student, Вы писали:
_>Имеется следующий класс.
_>_>class Log: public ofstream
_>{
_>public:
_> Log(char* szLogFn=LOG_FILE);
_> ~Log();
_> ofstream& operator<<(const char* szmsg);
_> ofstream& operator<<(int ival);
_> ofstream& operator<<(double dval);
_>private:
_> // ...
_>};
_>
...
_>Если в Log записывать числа, всё работает как надо, а для строк записывается опять число, и такое впечатление, что это адрес szmsg. Почему так происходит?
Потому что operator<< в std::ostream определен только для const void *, но не для const char *.
для вывода стандарнтных строк используется оператор определенный не в теле класса:
template<class _Traits> inline
basic_ostream<char, _Traits>& __CLRCALL_OR_CDECL operator<<(
basic_ostream<char, _Traits>& _Ostr,
const char *_Val)
Рискну посоветовать использовать:
class Log: public ofstream
{
public:
Log(char* szLogFn);
~Log();
template<class C>
Log& operator<<(const C& c)
{
EnterCriticalSection(&csLog);
static_cast<ofstream&>(*this) << c;
flush();
LeaveCriticalSection(&csLog);
return *this;
}
private:
};