B
Bob Altman
Hi all,
I'm writing a simple unmanaged C++ debug class (named, appropriately enough,
"debug") that lets me write text to a file. I use an overloaded << operator
to write text to the file, like this:
debug::Write() << "Hello" << ios::hex << 23 << ios::endl;
I've included a simplified version of my class definition below. My
question is this: I am using a templated function to implement the
overloaded << operator. When ios::endl is specified as the right hand
parameter to the << operator, I need to do some special processing. How can
I do this?
TIA - Bob
The class looks kind of like this (much simplified):
#include <iostream>
#include <fstream>
#include <iomanip>
class debug
{
private:
static debug m_debug;
static std:fstream m_outStream;
public:
// Begin a write operation to the file
static debug& Write()
{
// Open the file for append
m_outStream.open("debug.txt", ios:ut | ios::app);
// Write the beginning of the line
m_outStream << "Some text: ";
// Return a reference to a debug object
return m_debug;
}
// Overloaded << operator
template <class T> debug& operator<<(const T& param) const
{
if (param == ios::endl) { // This line obviously won't compile
<Do special processing to close the file>
} else {
// Write the parameter to the output stream
m_outStream << param;
}
return m_debug;
}
};
// Declare the static data
debug debug::m_debug;
std:fstream debug::m_outStream;
I'm writing a simple unmanaged C++ debug class (named, appropriately enough,
"debug") that lets me write text to a file. I use an overloaded << operator
to write text to the file, like this:
debug::Write() << "Hello" << ios::hex << 23 << ios::endl;
I've included a simplified version of my class definition below. My
question is this: I am using a templated function to implement the
overloaded << operator. When ios::endl is specified as the right hand
parameter to the << operator, I need to do some special processing. How can
I do this?
TIA - Bob
The class looks kind of like this (much simplified):
#include <iostream>
#include <fstream>
#include <iomanip>
class debug
{
private:
static debug m_debug;
static std:fstream m_outStream;
public:
// Begin a write operation to the file
static debug& Write()
{
// Open the file for append
m_outStream.open("debug.txt", ios:ut | ios::app);
// Write the beginning of the line
m_outStream << "Some text: ";
// Return a reference to a debug object
return m_debug;
}
// Overloaded << operator
template <class T> debug& operator<<(const T& param) const
{
if (param == ios::endl) { // This line obviously won't compile
<Do special processing to close the file>
} else {
// Write the parameter to the output stream
m_outStream << param;
}
return m_debug;
}
};
// Declare the static data
debug debug::m_debug;
std:fstream debug::m_outStream;