Can a function be accessible only to an include file ?

 

Can a function be accessible only to an include file ?

 
Lorentzos Roussos:

Can a function be accessible only to an include file ?

Wrap the include file in a class and make that function a private method😄

 

Not long before I started using OOP (and putting classes in mqh files) I asked MQ to add a scroll bar for the list of functions (Alt+M)😄 Because in large projects there were too many functions.

Using OOP and mqh files I don't need the list of functions anymore.

Without using OOP, it was very difficult to split the source code into header files. Because your functions are still separated from the data. Once you have combined data and methods into one object, there is no problem in putting it into a separate file.


This is what the EA file I'm currently working on looks like (same as any other EA's file)😄


 
Vladislav Boyko #:

Wrap the include file in a class and make that function a private method😄

If i put it inside the class you mean, the include ?  😄

damn it works ! wow thanks 

#property version   "1.00"
class has_abilities{
#include "the_ability.mqh";
      public:
 void check_me(int f){
      load_ability(f);
      }
};
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
  load_ability(f);
//---
   return(INIT_SUCCEEDED);
  }
 
Lorentzos Roussos #:

If i put it inside the class you mean, the include ?  😄

damn it works ! wow thanks 

No, that's not what I meant at all 😄😄😄

I meant to wrap the contents of the file in a class, not #inclule in the class description

 

For example, you have some mqh file containing 2 functions:

// Example.mqh

void functionA(double arg)
  {
   Alert(__FUNCTION__, " ", DoubleToString(arg, Digits()));
  }

void functionB(double arg)
  {
   Alert(__FUNCTION__, " ", DoubleToString(arg, Digits()));
  }

You want to add this function, but only have it available to functions in Example.mqh:

double secretFunction(double arg)
  {
   return(arg * 2.0);
  }

Just make the file an object and use encapsulation:

(Let's assume that the file contains a set of functions that do not use any data from the global scope. Therefore, we can make the methods static and not create an instance of the class later)

// Example.mqh

class CExample
  {
private:
   static double secretFunction(double arg);
public:
   static void   functionA(double arg);
   static void   functionB(double arg);
  };

void CExample::functionA(double arg)
  {
   Alert(__FUNCTION__, " ", DoubleToString(arg, Digits()));
  }

void CExample::functionB(double arg)
  {
   Alert(__FUNCTION__, " ", DoubleToString(arg, Digits()));
  }

double CExample::secretFunction(double arg)
  {
   return(arg * 2.0);
  }

secretFunction() is available for class methods:

void CExample::functionA(double arg)
  {
   Alert(__FUNCTION__, " ", DoubleToString(arg, Digits()));
   Alert("secret value ", secretFunction(arg));
  }

And not available for everything else:

#include "Example.mqh"

void OnStart()
  {
   CExample::functionA(1.234);             // OK
   Alert(CExample::secretFunction(1.234)); // 'CExample::secretFunction' - cannot access private member function
  }
 

You don't have to wrap the entire file in an object. You can only wrap code that uses a secret function in an object.

Just use encapsulation. To do this you will have to describe the object.

 

I hope you all realise that an include file is the equivalent of just copy/pasting text from one file into another.

There is no such thing as code that is only "accessible" in the include file.

The "solution" suggested is just a gimmick using OOP functionality in the declaration of class methods.

It has nothing to do with being "accessible" only to the included file.

The same OOP functionality would apply even if you copy/pasted that code directly into the main file.

 
Fernando Carreiro #:
The "solution" suggested is just a gimmick using OOP functionality in the declaration of class methods.

I would say that the author sets the task of concealment. Hiding is one of the objectives of encapsulation (although it would be incorrect to say that encapsulation is only hiding). Encapsulation, in turn, is an integral part of OOP. Therefore, from my point of view, Lorentzos Roussos set the task of implementing OOP functionality.

Fernando Carreiro #:
I hope you all realise that an include file is the equivalent of just copy/pasting text from one file into another.

Of course, header files are not a mandatory requirement for implementation.


An object-oriented idea was born in his head. But he is trying to apply it to a file, not an object

 
#define and #undef are an option. But personally, I am not a fan of the widespread use of macros.
 
Vladislav Boyko #:

You don't have to wrap the entire file in an object. You can only wrap code that uses a secret function in an object.

Just use encapsulation. To do this you will have to describe the object.

Thats what i wanted to avoid xD 

The main issue is i did not want to edit old bad code bad i'll have to eventually 

Reason: