OOP: How to have a static array of singleton classes?

 

In a static StrategyManager class I want to have a static array of Strategy Singleton classes who's functions i call depending on the current market's behavior. The static functions like OnInit() in the StrategyManager class are being called in the Expert's mq4 file.
When I try to compile this, the compiler returns the following error: "const expression required". The error is pointing towards the last line of the StrategyManager.mqh class (see below).

I need to 'resolve' the strategy array because it is static. But I can not use {} to assign data to it. How am I suppose to do that then? Thanks!


Strategy.mqh

class Strategy
{
private:
    static Strategy* m_instance;
           string    m_name;
protected:
    void Strategy();
public:
    static Strategy* GetInstance();

virtual
    void OnInit() {};
    void OnTick() {};
};

Strategy* Strategy::m_instance = NULL;
Strategy::Strategy() : m_name("None") {}

static Strategy* Strategy::GetInstance()
{
    if(m_instance == NULL)
        m_instance = new Strategy();
    return m_instance;
}

 

StrategyRanging.mqh

#include "./Strategy.mqh"

class StrategyRanging : public Strategy
{
private:
    static StrategyRanging* m_instance;
public:
    static StrategyRanging* GetInstance();

    void StrategyRanging();
};

StrategyRanging* StrategyRanging::m_instance = NULL;
StrategyRanging::StrategyRanging() : m_name("Ranging") {}

static StrategyRanging* StrategyRanging::GetInstance()
{
    if(m_instance == NULL)
        m_instance = new StrategyRanging();
    return m_instance;
}


StrategyManager.mqh

#include "./strategies/Strategy.mqh"
#include "./strategies/StrategyRanging.mqh"

class StrategyManager()
{
private:
    static Strategy* m_strategy[1];
public:
    void   StrategyManager() {};
    void  ~StrategyManager() {};
};

Strategy* StrategyManager::m_strategy[1] = { StrategyRanging::GetInstance() }; // <- does not work.. compiler returns "const expression required"
 
That doesn't make sense to me, why do you want a singleton to use an array of objects after that ?
 
angevoyageur:
That doesn't make sense to me, why do you want a singleton to use an array of objects after that ?

I do not want one file with 6000 lines of code that switches between strategies.  For now, the array of objects contains only one strategy but more can be added. Then I select one of them to run based on the market conditions.

I could use static classes but those cannot inherit..  

Maybe my design is wrong. If so, can you provide some alternative?

 
niquedegraaff:

I do not want one file with 6000 lines of code that switches between strategies.  For now, the array of objects contains only one strategy but more can be added. Then I select one of them to run based on the market conditions.

I could use static classes but those cannot inherit..  

Maybe my design is wrong. If so, can you provide some alternative?

In my opinion your design is wrong yes. Forget the singleton and proceed with your arrays.

A singleton should be ... a singleton, obviously a strategy is not a singleton.

Why do you want the Strategy class to be a singleton ? Maybe I am missing something.

 
angevoyageur:

In my opinion your design is wrong yes. Forget the singleton and proceed with your arrays.

A singleton should be ... a singleton, obviously a strategy is not a singleton.

Why do you want the Strategy class to be a singleton ? Maybe I am missing something.

agreed
class Basic_Strategy{
 protected:                                   // Abstract
   Basic_Strategy(){}                            // Ctor
 public: // common to all
   void on_tick(){ do_on_tick(); }
 private: // customizable
   virtual void do_on_tick(){ /* pure or default */ }
};
class Strategy1 : public Basic_Strategy{
 private:
   virtual void do_on_tick(){ ... }
};
class Strategy2 : public Basic_Strategy{ ... };

Basic_Strategy* strategies[99];  uint nStrategies = 0;
:
strategies[nStrategies++] = new Strategy2();
void OnDeinit(){
   while(nStrategies > 0) delete strategies[--nStrategies];