Is it true - there is no way .. - page 2

 

Maybe you can try

name.c_str()
 
Carl Schreiber:

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

My previous method works fine, and typename(this) works fine, too. For example:

class CMyClass
{
private:

//...  your private variables and methods
string            m_name;

public:
                  CMyClass(void)  {  m_name = typename(this);  }     // constructor
                 ~CMyClass(void)  {}     // destructor

string            getClassName(void)  {   return(m_name); }
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);
}

But, if you want to create a generic function to get the type of any variable or structure or class, you can use a template like this. You would only have to pass your variable or structure or class instance to GetName function as a parameter:

class CMyClass
{
private:

//...  your private variables and methods

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


protected:

//... your protected variables and methods

};

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

CMyClass mycl;
string   classname;

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

template<typename T>
string GetName(const T &t)
  {

   return(typename(T));
//---
  }
  
//===========================================================

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

   return(INIT_SUCCEEDED);
}


Regards.

 

in MQL4this or any other pointer to an instance of a class like

m_name = typename(this); 

creates an compiler error: '' - invalid cast operation.

BTW, the service desk answered: "Thanks for your suggestion, but we have no such plans."

 
Carl Schreiber:

in MQL4this or any other pointer to an instance of a class like

creates an compiler error: '' - invalid cast operation.

BTW, the service desk answered: "Thanks for your suggestion, but we have no such plans."

I don´t know what Service Desk said. The only thing I know is both of them are working fine in mql4 (Metatrader 4 build 1012, Metaeditor build 1395). Which is your MT4 build??- Did you test them??. You can test them, instead of asking Service Desk.



You have 2 EA examples attached. If your MT4 build isn't 1012, please don't recompile them.  Regards-

Files:
MyClass.mq4  2 kb
MyClass.ex4  6 kb
MyClass2.mq4  2 kb
MyClass2.ex4  6 kb
 

An another ejample: MyClass3.mq4.  Now storing m_name variable inside the constructor. Regards.

Files:
MyClass3.mq4  2 kb
MyClass3.ex4  6 kb
 
Jose Francisco Casado Fernandez:

An another ejample: MyClass3.mq4.  Now storing m_name variable inside the constructor. Regards.

Agree.

I don't understand the problem either. And by the way why do one want a class name inside a class ?

 
Alain Verleyen:

Agree.

I don't understand the problem either. And by the way why do one want a class name inside a class ?

I don't know. It's a question for Carl. He said that typename(this) doesn't work in mql4, and I proved it's working fine. Now, he have the solution (3 different solutions), and he will be able to use like he wants. Regards
 
Jose Francisco Casado Fernandez:
I don't know. It's a question for Carl. He said that typename(this) doesn't work in mql4, and I proved it's working fine. Now, he have the solution (3 different solutions), and he will be able to use like he wants. Regards

I want to be able to use my OOP-code by a script, an EA and e.g. as the heart of an indicator. Due to the options of virtual methods and method overloading it can happen that - while developing - you 'land' in the wrong method or to be precise in the method with the same name but of the wrong (e.g. base-) class. In the debugger it is easy to detect, but at least in MQL4 you can't really debug an indicator, so you have to show it either by Print or Comment().

I don't know what has been a problem on my side and why I have got the compiler error (s.th. like "the conversion is not allowed") - it must have been my error - but now typename(this) works - thanks!

 
Thank you
Reason: