file operation using class constructor

 

Hi, I want to ask about file operation inside class

Consider this is the basic code that I copy from here (MQL4 FileOpen)

//--- correct way of working in the "file sandbox"
ResetLastError();
filehandle=FileOpen("fractals.csv",FILE_WRITE|FILE_CSV);
if(filehandle!=INVALID_HANDLE)
  {
    FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period)));
    FileClose(filehandle);
    Print("FileOpen OK");
  }


And,, I remake it into class. But this time the code a bit different

class CFileOperation {
  protected:
    int filehandler;

  public:
    CFileOperation() {
      filehandle=FileOpen("fractals.csv",FILE_WRITE|FILE_CSV);
      do_job();
    }
    ~CFileOperation() {
      FileClose(filehandle);
    }

    void do_job() { /* MULTIPLE FUNCTION */ }            
    void do_demo() {
      FileWrite(filehandle,TimeCurrent(),Symbol(), EnumToString(ENUM_TIMEFRAMES(_Period)));
    }
};

void OnStart() {
        CFileOperation *OP = new CFileOperation();  // Calling constructor

        OP.do_demo();

        delete OP;  // Call deconstructor

        //--- Rest of the code
}


**Please ignore if I do not place the class parameter/payload, error handling & etc

**or even if there's mistake on code such as forget semicolon, class expect param but I do not pass & etc. Because that's not the question


Question:

From what we can see here, `FileOpen` is placed on constructor & `FileClose`is place on de-constructor. It is OK or bad practice if I do that?


The idea behind this, because after the FileOpen, I have to run a few function before the file need to be close.

Since the function itself too long and I plan to split into it's individual function in class and call it at once on constructor so that the code can easily read & troubleshooting.

 
  1. Mohd Syafiq Johar Arrifin: It is OK or bad practice if I do that?
            CFileOperation *OP = new CFileOperation();  // Calling constructor
    
            OP.do_demo();
    
            delete OP;  // Call deconstructor

    The only bad practice is to do dynamic allocation instead of letting the compiler handle it.

            CFileOperation OP;  // Calling constructor
            OP.do_demo();
         } // Call deconstructor
  2. In addition, generalize your code.

      public:
        CFileOperation(string filename) {
          filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
    ///////////////
    CFileOperation OP("fractals.csv");  // Calling constructor
 

Thank you William Roeder.


`1. The only bad practice is to do dynamic allocation instead of letting the compiler handle it.`

Did you mean from this,, the bad practice that currently I'm doing is to use function pointer instead of:

/* let [METHOD1] = */

        CFileOperation OP;  // Calling constructor
        OP.do_demo();
     } // Call deconstructor

And, that mean there's no problem to code FOpen in constructor & FClose in deconstructor and this kind of code is safe to use without causing any bug/problem?

I already forget this method name/keyword since not using it for a long time but I will refer this as METHOD1 for temporary, and I think it's a good time to ask & recap..


- What is the different between [METHOD1] & function pointer?

- When to use idle METHOD1 or function pointer..

Reason: