Is it true - there is no way ..

 
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?
 
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. 

 

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.

 
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()
 
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!

 
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 ?
 
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.

 
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.

 
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);
...
}
 
 
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);
}
 
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 ..

Reason: