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 methodsstring m_name;
public:
CMyClass(void) { m_name = typename(this); } // constructor
~CMyClass(void) {} // destructorstring getClassName(void) { return(m_name); }
protected:
//... your protected variables and methods
};
//===========================================================
CMyClass mycl;
string classname;
intOnInit()
{
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 methodspublic:
CMyClass(void) {} // constructor
~CMyClass(void) {} // destructorprotected:
//... your protected variables and methods
};
//===========================================================
CMyClass mycl;
string classname;
//===========================================================template<typename T>
string GetName(const T &t)
{
return(typename(T));
//---
}
//===========================================================intOnInit()
{
classname = GetName(mycl);
Print("The name of my class = " + classname);
return(INIT_SUCCEEDED);
}
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-
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!
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Maybe you can try
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:
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:
Regards.
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."
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-
An another ejample: MyClass3.mq4. Now storing m_name variable inside the constructor. Regards.
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 ?
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
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!