Inheritance and polymorphism

 

Hi,

I am new to developing OOP in MQL4 (not to OOP :) ) and run into the following when testing this base:

class BaseObject
 {
   public:
     virtual string ToString()
     {
        return TypeName();
     }
     virtual string TypeName()
     {
        return typename(this);
     }
};

class MyNewClass : public BaseObject {};

int OnInit()
  {
   MyNewClass tmp;
   Comment("The typename of MyNewClass is ", tmp.ToString());
   return(INIT_SUCCEEDED);

  }

I am expecting this to say: "The typename of MyNewClass is MyNewClass", instead it says it is of the type BaseObject?

Is there something I am doing wrong here? How should it be done without acctually implementing the overrride in MyNewClass?

Thanks,

Victor.

 
vkno422:
class BaseObject
 {
   public:
     virtual string ToString(){    return TypeName(); }
     virtual string TypeName(){ return typename(this); }
};

class MyNewClass : public BaseObject {};
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Always make virtual functions private. The public methods (inheritance) call the private ones. The Override provide the polymorphism.
  3. Since the virtual wasn't overridden the call is BaseObject::TypeName() and this is a BaseObject. Thus it doesn't work.
  4. I previously posted my dynamic cast code. It uses a macro. Attached is the latest. Example:
    #include "polymorphic.mqh"       // cPolymorphic
    
    class cObservable : public cObserver{  // Allow observable to also observe.
     public:       POLYMORPHIC(cObserver);
       :

Files:
 

Ok, I noticed that it does work when overriding the TypeName function.

But I don't want to 'have to' implement the base class functions that need to be used polymorphically every time.

I guess this is the 1st implementation attempt of Mql4 of OOP and there is quite some room for improvement.

Also I am not too thrilled about the implementation of properties.

Being used to C#


Thanks William

 
vkno422: I guess this is the 1st implementation attempt of Mql4 of OOP and there is quite some room for improvement.
It has nothing to do with mql4. It is the definition of OOP. If you do not override a function, you get the base class version. If you do not override a virtual function, you get the base class version.
 
WHRoeder:
vkno422: I guess this is the 1st implementation attempt of Mql4 of OOP and there is quite some room for improvement.
It has nothing to do with mql4. It is the definition of OOP. If you do not override a function, you get the base class version. If you do not override a virtual function, you get the base class version.

Actually the implementation has much more to do with MQL4 than OOP. If this function was implemented in JAVA, which is pure OOP language in comparison to C++ or MQL4, then it would work as the original poster expected.

WHRoeder:

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. Always make virtual functions private. The public methods (inheritance) call the private ones. The Override provide the polymorphism.
  3. Since the virtual wasn't overridden the call is BaseObject::TypeName() and this is a BaseObject. Thus it doesn't work.
  4. I previously posted my dynamic cast code. It uses a macro. Attached is the latest. Example:

Could you post your code how you can override the private method? I am just curious, because I cannot.

 
DeepThought:

Actually the implementation has much more to do with MQL4 than OOP. If this function was implemented in JAVA, which is pure OOP language in comparison to C++ or MQL4, then it would work as the original poster expected.

Could you post your code how you can override the private method? I am just curious, because I cannot.


  1. Write some Java and post the output. Prove it.
  2. #property strict
    class base{
       public:  void test(void){ vpm(); }
       private: virtual void vpm(void){ Print("base::vpm"); }
    };
    class derv : public base{
       private: virtual void vpm(void){ Print("derv::vpm"); }
    };
    void OnStart(){
       base* pb = new derv;
       pb.test();
    }
    
    
    2014.09.07 14:15:25.369    Script tests USDCHF,H1: removed
    2014.09.07 14:15:25.351    tests USDCHF,H1: 16 bytes of leaked memory
    2014.09.07 14:15:25.351    tests USDCHF,H1: 1 object of type derv left
    2014.09.07 14:15:25.351    tests USDCHF,H1: 1 undeleted objects left
    2014.09.07 14:15:25.351    tests USDCHF,H1: uninit reason 0
    2014.09.07 14:15:25.351    tests USDCHF,H1: Alert: derv::vpm
    2014.09.07 14:15:25.351    tests USDCHF,H1: initialized
 
DeepThought:

JAVA, which is pure OOP language in comparison to C++

:)
Reason: