Search code examples
superclassobject-initializersmql5

How to initialize class and override its parent in MQL?


I've two classes: Chart which extends Market.

I'd like to initialize the Chart class, but providing the pointer to already existing parent class to save some memory (to avoid initialization of new instances if it can point to the same thing).

Here is the code:

class Market {
  public:
    void Market(Market *_market) {
      this = GetPointer(_market); // Error: '=' object required
    }
};

class Chart : public Market {
  public:
    void Chart(Market *_market) : Market(_market) {
    }
};

however it fails with:

'=' object required

Is is possible to override instance of a parent class during child initialization by giving class pointer?


Solution

  • This should work by overriding pointer to the parent class in the constructor:

    class Market {
    };
    
    class Chart : public Market {
      public:
        void Chart(Market *_market) {
          Market *_parent = (Market *) GetPointer(this);
          _parent = _market;
        }
    };