mercredi 19 octobre 2016

dynamic_cast from ostream to ofstream is causing segmentation fault

In the code below I am trying to create a Logging application which outputs to either cout or to a selected file.

  1. I used ostream to hold either a ofstream object or cout.
  2. in the Destructor of CLog i want to close the file if ostream object is not cout.
  3. 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