Hi,
I think im trying to break c++ again in making it do something it does not want to do. Basically i have a logging class im making which simply takes a string stream and pumps it into a file. But i want 2 different versions one that trys to force the data to disk and one that doesn't care when it gets written to disk.
To achieve this and save lots of if's i thought id try function pointers. but the thing is i want my function pointer public and the other 2 functions hidden away so the user cannot call them.
how i have tried this below.
/* logger.h */
class Logger
{
public:
bool SetLogParams(bool diskAtOnce);
bool CreateLog(const std::string &name, bool overwrite = true);
void CloseLog(void);
typedef void (Logger::*FuncPtr)(const std::stringstream &);
FuncPtr Write;
protected:
void WriteLogS(const std::stringstream &stream);
void WriteLog(const std::stringstream &stream);
std::ofstream log;
};
/* logger.cpp*/
bool Logger::SetLogParams(bool diskAtOnce)
{
if(diskAtOnce)
{
Write = &WriteLogS;
}
else
{
Write = &WriteLog;
}
}
void Logger::WriteLog(const std::stringstream &stream)
{
log<<stream;
}
void Logger::WriteLogS(const std::stringstream &stream)
{
log<<stream;
log.flush();
}
As i think you can see i basically want the logger to be able to be set in either mode (at any time but i only intend to use it once at the start) and i call the function pointed to by my function pointer such as
stringstream error;
error<<"Error occoured: "<<err.no<<err.details<<endl<<endl;
myLog.Write(error);
So that i get the same name to call to write to the log but internally it knows based on my init call which function to use.
The error im geting atm is a compiler one "error C2276: '&' : illegal operation on bound member function expression"
I think this is telling me i cant make public pointer to protected members, so my actual question after all of this is can anyone advise me how to get the interface i want but in the way c++ wants me to do it?
Thanks in advance again!!