I noticed that from a derived class I can't access protected functions of an object from its base class (a pointer to other object), however from the base class I can access private functions of the other object (of the same type), and more strangely, from the derived class I can access and modify protected members from the base class directly, which seems contradictory to me.
Is it a bug? Or is it something to be expected and has some reasoning to it (which I'm curious to know I must say)?
This should not compile, as "otherPointer.ProtectedFunction()". So that's a bug.
otherPointer.protectedMember = 2;
Other things are ok.
This should not compile, as "otherPointer.ProtectedFunction()". So that's a bug.
Other things are ok.
Because this works only inside the same scope of classes and inheritance structure.
If it's supposed to work like C++ then it should compile. Strange but fine.
Why would the protected variable compiles while the protected method doesn't ? Both should not compile.
If it's supposed to work like C++ then it should compile. Strange but fine.
A protected member of a class is only accessible
So I created some test code to check what it gives in MQL, compared to C++ :
//+------------------------------------------------------------------+ //| Compiler checking access | //+------------------------------------------------------------------+ class CDerived; //--- class CBase { private: int privateMember; void privateMethod(); protected: int protectedMember; void protectedMethod(); public: void AccessPrivate(CBase *ptr); void AccessPrivate(CDerived *ptr); void AccessProtected(CBase *ptr); void AccessProtected(CDerived *ptr); }; //--- class CDerived : public CBase { public: void AccessPrivateD(CBase *ptr); void AccessPrivateD(CDerived *ptr); void AccessProtectedD(CBase *ptr); void AccessProtectedD(CDerived *ptr); }; //--- void CBase::privateMethod() {}; //--- void CBase::protectedMethod() {}; //--- void CBase::AccessPrivate(CBase *ptr) { int dummy = ptr.privateMember; ptr.privateMethod(); } //--- void CBase::AccessPrivate(CDerived *ptr) { int dummy = ptr.privateMember; ptr.privateMethod(); } //--- void CBase::AccessProtected(CBase *ptr) { int dummy = ptr.protectedMember; ptr.protectedMethod(); } //--- void CBase::AccessProtected(CDerived *ptr) { //int dummy = ptr.protectedMember; // NOT OK in MQL, it should compile : cannot access to protected member 'protectedMember' declared in class 'CBase'. Compile in C++. ptr.protectedMethod(); } //--- void CDerived::AccessPrivateD(CBase *ptr) { //int dummy = ptr.privateMember; // OK, C++ error: ‘int CBase::privateMember’ is private within this context //ptr.privateMethod(); // OK, C++ error: ‘void CBase::privateMethod()’ is private within this context } //--- void CDerived::AccessPrivateD(CDerived *ptr) { //int dummy = ptr.privateMember; // OK, C++ error: ‘int CBase::privateMember’ is private within this context //ptr.privateMethod(); // OK, C++ error: ‘void CBase::privateMethod()’ is private within this context } //--- void CDerived::AccessProtectedD(CBase *ptr) { int dummy = ptr.protectedMember;// NOT OK Compile in MQL but it should NOT, C++ error: ‘int CBase::protectedMember’ is protected within this context //ptr.protectedMethod(); // OK, C++ error: ‘void CBase::protectedMethod()’ is protected within this context } //--- void CDerived::AccessProtectedD(CDerived *ptr) { int dummy = ptr.protectedMember; ptr.protectedMethod(); }
So there are 2 inconsistencies, that's MQL bugs. I asked explanation to MQ.
So I created some test code to check what it gives in MQL, compared to C++ :
So there are 2 inconsistencies, that's MQL bugs. I asked explanation to MQ.
void CBase::AccessProtected(CDerived *ptr) { //int dummy = ptr.protectedMember; // NOT OK in MQL, it should compile. Compile in C++ cannot access to protected member 'protectedMember' declared in class 'CBase' ptr.protectedMethod(); }
ptr.protectedMethod();
//int dummy = ptr.protectedMember; // NOT OK in MQL, it should compile. Compiles in C++; MQL: cannot access to protected member 'protectedMember' declared in class 'CBase'

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I noticed that from a derived class I can't access protected functions of an object from its base class (a pointer to other object), however from the base class I can access private functions of the other object (of the same type), and more strangely, from the derived class I can access and modify protected members from the base class directly, which seems contradictory to me.
Is it a bug? Or is it something to be expected and has some reasoning to it (which I'm curious to know I must say)?