Very bad idea. Why do you want to do that ?
Agreed, bad idea. You should never call them.
“When should virtual functions be public, protected, or private?” The short answer is: Rarely if ever, sometimes, and by default, respectively.
Virtuality - Guru of the Week 9 September 2001.
Agreed, bad idea. You should never call them.
“When should virtual functions be public, protected, or private?” The short answer is: Rarely if ever, sometimes, and by default, respectively.
Virtuality - Guru of the Week 9 September 2001.
Hey y'all
I'm facing a rather frustrating challenge. Consider the following:
- CAdmin is a parent class.
- CNotify inherets CAdmin, therefore, is a child from CAdmin.
- Both classes have a function called Function(void).
- CAdmin declaration of Function(void) is virtual.
- CNotify declaration of Function(void) is not virtual.
Given the following code:
And the following declaration of both classes:
How can I properly call the CAdmin version of Function() instead of the CNotify, given that I create only one object of type CNotify?
You are going to get into a complete mess if you do that sort of thing... BUT, the reason your code was not working is because you had not provided function bodies for the constructor/deconstructor
class CAdmin { public: CAdmin(void) {return;}; ~CAdmin(void) {return;}; virtual void Function() { Print("CAdmin"); } }; class CNotify : public CAdmin { public: CNotify(void) {return;}; ~CNotify(void) {return;}; void Function() { Print("CNotify"); } }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { CNotify notify; notify.Function(); notify.CAdmin::Function(); }
You are going to get into a complete mess if you do that sort of thing... BUT, the reason your code was not working is because you had not provided function bodies for the constructor/deconstructor
Thanks for the info!
I agree with the fact that most likely by using that it will result into a mess as far as how it's processed. Still, I'd like to know if it is possible, since when I try to compile the suggested code, I still get this error:
'Function' - access to non-static member or function Class inheritance test.mq4 36 16
Thanks for the info!
I agree with the fact that most likely by using that it will result into a mess as far as how it's processed. Still, I'd like to know if it is possible, since when I try to compile the suggested code, I still get this error:
the code I posted works fine and does what you asked.
just copy this exactly it is a script..
output
if you still get an error post back your exact code with the error detail
//+------------------------------------------------------------------+ //| Scratch.mq5 | //+------------------------------------------------------------------+ #property strict class CAdmin { public: CAdmin(void) {return;} ~CAdmin(void) {return;} virtual void Function() { Print("CAdmin"); } }; class CNotify : public CAdmin { public: CNotify(void) {return;} ~CNotify(void) {return;} void Function() { Print("CNotify"); } }; //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { CNotify notify; notify.Function(); notify.CAdmin::Function(); }
Somehow I can't get it to compile. Hope this screenshot helps:
//+------------------------------------------------------------------+ //| CallBase.mq4 | //| Copyright 2020, Rosh Jardine Capital | //| https://www.roshjardine.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, Rosh Jardine Capital" #property link "https://www.roshjardine.com" #property version "1.00" #property strict class CAdmin { public: CAdmin(void); ~CAdmin(void); virtual void Function() { Print("CAdmin"); } }; CAdmin::CAdmin(void) { } CAdmin::~CAdmin(void) { } class CNotify : public CAdmin { public: CAdmin m_parent; CNotify(void); ~CNotify(void); void Function() { Print("CNotify"); } }; CNotify::CNotify(void) { } CNotify::~CNotify(void) { } //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { CNotify notify; notify.Function(); notify.m_parent.Function(); }you should declare a class member as parent object in public scope..but you've been told..it's bad idea.
Somehow I can't get it to compile. Hope this screenshot helps:
You never said you were on MT4....
I get the same error on MT4 clearly the OOP implementation is different in MQL4
time to let a bad idea go :)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey y'all
I'm facing a rather frustrating challenge. Consider the following:
Given the following code:
And the following declaration of both classes:
How can I properly call the CAdmin version of Function() instead of the CNotify, given that I create only one object of type CNotify?