In the code below I am trying to create a Logging application which outputs to either cout or to a selected file.
- I used ostream to hold either a ofstream object or cout.
- in the Destructor of CLog i want to close the file if ostream object is not cout.
-
but dynamic_cast from ostream to ofstream is causing segmentation fault.
what can be the issue that is causing Segmentation Fault ?CLog(std::ostream & streamObj = std::cout); CLog::CLog(std::ostream & streamObj) { } CLog::~CLog() { if (pStreamObj != NULL && &(std::cout)!= pStreamObj) //if the streamObj is a file stream { dynamic_cast<std::ofstream&>(*pStreamObj).close(); delete pStreamObj; } }
Aucun commentaire:
Enregistrer un commentaire