do we need to separate the function implementation from the class?

 

I understand that such feature is available in cpp,  because the `.h` file and `.cpp` file can be separate and it's to make sure when you include a `.h` file you don't recreate the functions again.

but I didn't see any explanation about why in MQL also we need to separate the function implemenation of the the class functions.


in MQL what will happen when one class get import into two files and those two files get import to the main script? does that one class get recreated again and again so we should separate the function implementation and their declaration?


if not why even bother?
 
  1. In cpp, you compile separate files, and then combine them with the linker to create an executable. There is no linker in MTx, you just compile all sources together.
  2. Each included file will be read only once; include guards are not needed.
  3. You can separate definition and implementation if you want to, but the definition will have to include the implementation file. Why bother?
 
William Roeder #:
  1. In cpp, you compile separate files, and then combine them with the linker to create an executable. There is no linker in MTx, you just compile all sources together.
  2. Each included file will be read only once; include guards are not needed.
  3. You can separate definition and implementation if you want to, but the definition will have to include the implementation file. Why bother?


thanks for the through answer, this helps a lot. just wanted to make sure to save my time without making any memory leak. I despise to code in this style

class B{
    B();
    SomeFunc(); 
}
B:B(){
    //Implementation goes here
}
B::SomeFunc(){
    //implementation goes here
}

and prefer this.

class A{
    A(){
        //implementation goes here
    }
    SomeFunc(){
        //implementation goes here
    }
}


like any other modern language save time and sense by not spending my life on scrolling up and down and maintaining the same deceleration twice. it does have a merit in cpp but with your explanation it's clear that doesn't have any standing in MQL. 

 
mh_amri #:


and prefer this.

class A{
    A(){
        //implementation goes here
    }
    SomeFunc(){
        //implementation goes here
    }
}

like any other modern language save time and sense by not spending my life on scrolling up and down and maintaining the same deceleration twice. it does have a merit in cpp but with your explanation it's clear that doesn't have any standing in MQL. 

The latter is ok to do so in MQL, I also prefer to code like this. It doesn't create a 'memory leak' whatsoever and works well with include files.

 
mh_amri #:


thanks for the through answer, this helps a lot. just wanted to make sure to save my time without making any memory leak. I despise to code in this style

and prefer this.


like any other modern language save time and sense by not spending my life on scrolling up and down and maintaining the same deceleration twice. it does have a merit in cpp but with your explanation it's clear that doesn't have any standing in MQL. 

That's not correct. In this article, an example where it is useful to separate definition and implementation.