Download MetaTrader 5

Is it true - there is no way ..

To add comments, please log in or register
They are making profit already using Freelance service. You can join them!
Carl Schreiber
6591
Carl Schreiber 2016.10.06 18:50 
to get the name of the class (like EnumToString()) except by the file name - which fails as soon as you have (to have) two or more classes within the same file?
Drazen Penic
2544
Drazen Penic 2016.10.06 20:10  
Carl Schreiber:
to get the name of the class (like EnumToString()) except by the file name - which fails as soon as you have (to have) two or more classes within the same file?

As far as I know, there is no such a function in the MQL. You should create your own mechanism.

CObject class has virtual method Type() that returns an integer.

You can use CObject as a base class for all your other classes and in each class override Type method.

Or you can create your base class "CMyBaseObject" and create virtual TypeName() method that returns string. 

Jose Francisco Casado Fernandez
2407
Jose Francisco Casado Fernandez 2016.10.06 20:30  

If you want to get a string with the name of the method inside a class, you can use __FUNCTION__ macro. If you want to get a full signature of the method, you can use __FUNCSIG__ macro.

If you want to get that string from outside the class, you can do like Drazen says. Regards.

Alain Verleyen
Moderator
30742
Alain Verleyen 2016.10.06 21:09  
Carl Schreiber:
to get the name of the class (like EnumToString()) except by the file name - which fails as soon as you have (to have) two or more classes within the same file?
typename()
Carl Schreiber
6591
Carl Schreiber 2016.10.06 21:58  
Alain Verleyen:
typename()

my naive approach typename(this) within a method of a class isn't working, and the extra definitions with template is too complicated for what I need it.

So I'll stick to __FILE__ - but thanks anyway!

Alain Verleyen
Moderator
30742
Alain Verleyen 2016.10.06 22:14  
Carl Schreiber:

my naive approach typename(this) within a method of a class isn't working, and the extra definitions with template is too complicated for what I need it.

So I'll stick to __FILE__ - but thanks anyway!

What is your goal ?
Jose Francisco Casado Fernandez
2407
Jose Francisco Casado Fernandez 2016.10.06 23:02  
Carl Schreiber:

my naive approach typename(this) within a method of a class isn't working, and the extra definitions with template is too complicated for what I need it.

So I'll stick to __FILE__ - but thanks anyway!

You can create a new method at public section of the class, like this:

string            getClassName(void)  {
                                            string name = __FUNCTION__;
                                            int pos = StringFind(name, "::", 0);
  
                                            return(StringSubstr(name, 0, pos));
                                      }


Regards.

Carl Schreiber
6591
Carl Schreiber 2016.10.07 08:41  
Alain Verleyen:
What is your goal ?

Just imagine you have a class of indicators and a class of expert and with that you know what is is without knowing the names all by heart.

Carl Schreiber
6591
Carl Schreiber 2016.10.07 08:54  
Jose Francisco Casado Fernandez:

You can create a new method at public section of the class, like this:


Regards.

You function returns just getClassName - its function.

It would work if you place the code directly in the constructor:

CMyClass::CMyClass(...) {
...
       name = __FUNCTION__;
       int pos = StringFind(name, "::", 0);
       name = StringSubstr(name, 0, pos);
...
}
 
Jose Francisco Casado Fernandez
2407
Jose Francisco Casado Fernandez 2016.10.07 10:30  
Carl Schreiber:

You function returns just getClassName - its function.

It would work if you place the code directly in the constructor:

 

No need to place code inside constructor, you can place it on public section and call it from outside the class. Note that method DOESN'T return directly the function name, but only the part before '::' symbols. So, it's the name of the class. Regards-

class CMyClass
{
private:

//...  your private variables and methods

public:
                  CMyClass(void)  {}     // constructor
                 ~CMyClass(void)  {}     // destructor

string            getClassName(void)  {
                                            string name = __FUNCTION__;
                                            int pos = StringFind(name, "::", 0);
  
                                            return(StringSubstr(name, 0, pos));
                                      }
protected:

//... your protected variables and methods

};

//===========================================================

CMyClass mycl;
string   classname;

int OnInit()
{
   classname = mycl.getClassName();
   
   Print("The name of my class = " + classname); 

   return(INIT_SUCCEEDED);
}
Carl Schreiber
6591
Carl Schreiber 2016.10.07 10:59  
Jose Francisco Casado Fernandez:

No need to place code inside constructor, you can place it on public section and call it from outside the class. Note that method DOESN'T return directly the function name, but only the part before '::' symbols. So, it's the name of the class. Regards-

I declare a name as a class variable and want to assign its value at construction time. And due to my coding principle small is beautiful and smaller is even more beautiful I tried to use one command and defined the function as a 'global' function so that it is available to any class even if they do not exist yet - and that fails. :(

The best solution from my viewpoint still would be - as I tried and failed - typename(this).

I posted it at the service desk yesterday - it might take a while, if ..

12
To add comments, please log in or register