Apply base class pointer to child class - page 2

 
Vladislav Boyko #:

I'm not sure I understand your question correctly.

I sometimes have a situation where child classes are initialized differently. I did something like this:

Code just to demonstrate the idea. Of course, I did not run it and did not test it.

Thank you Vladislav for your answer. 

I'd like to do the other way round, 

//this I can't change because it's from an external library (standard library), 
Cparent instance = new Parent(); 

//convert type somehow
//some code

double someVar = instance.Launch_Method_from_child_class_that_isnt_in_parent_class();
 
Dominik Christian Egert #:
You could do a cast of the pointer.

This is working also, thanks !

that means each time I want to use a child method I have 2 solutions right :

//defining a new variable when m_trailing is already instanciated:
m_trail = m_trailing;

//then I could have something like this
m_trail.A(someparameter);
m_trail.B(someparameter);
m_trail.C(someparameter);

//OR

//if there is little code to add (if not looks messy)
((CExpertModelTrailing *)m_trailing).A(someparameter);
((CExpertModelTrailing *)m_trailing).B(someparameter);
((CExpertModelTrailing *)m_trailing).C(someparameter);

 
DidMa #:

This is working also, thanks !

that means each time I want to use a child method I have 2 solutions right :

That's a bad solution.

Your problem come from this class CExpertModelTrailing which you don't need at all. It does nothing except bothering you.

 
Alain Verleyen #:

That's a bad solution.

Your problem come from this class CExpertModelTrailing which you don't need at all. It does nothing except bothering you.

Thanks for your reply. 
I guess you're saying that I don't need this intermediary CExpertModelTrailing class because in the standard library the TrailingXXX.mqh are inheriting for the base CExpertTrailing class. I changed that so that all my Trailing classes are extending the CExpertModelTriling class.

How else could I do than this (below code) to create my own code instanciated dynamically

#include <Expert\ExpertBase.mqh>
#include <Expert\ExpertTrailing.mqh>

//+------------------------------------------------------------------+
//|                                                                   |
//+------------------------------------------------------------------+
class CExpert_Tparams
  {
public:
   int               m_TrailingBehaviour;
   
   void              CExpert_Tparams() {}
   void             ~CExpert_Tparams() {}

  };

//existing code in standard library, so not modifiable
class CExpert : public CExpertBase
  {
protected:
   // [...] some other code
   CExpertTrailing   *m_trailing;                 // trailing stops object

   // [...] some other code
   virtual bool      InitTrailing(CExpertTrailing *trailing = NULL);
  };
//+------------------------------------------------------------------+
//| Initialization trailing object                                   |
//+------------------------------------------------------------------+
bool CExpert::InitTrailing(CExpertTrailing *trailing)
  {
   if(m_trailing != NULL)
      delete m_trailing;
//---
   if(trailing == NULL)
     {
      if((m_trailing = new CExpertTrailing) == NULL)
         return(false);
     }
   else
      m_trailing = trailing;
//--- initializing trailing object
   if(!m_trailing.Init(GetPointer(m_symbol), m_period, m_adjusted_point))
      return(false);
   m_trailing.EveryTick(m_every_tick);
   m_trailing.Magic(m_magic);
//--- ok
   return(true);
  }

// My Code
//+------------------------------------------------------------------+
class CExpertModelTrailing : public CExpertTrailing
  {
protected:
   double                     m_TrailingActivate;                             // Trailing activate if profit is >=   
public:
   double                     SetTrailingActivate(double activate)            { m_TrailingActivate = activate; return m_TrailingActivate; }
   double                     GetTrailingActivate()      { return m_TrailingActivate; }
   
   // let's say CExpert_Tparams class contains all the trailing parameters given by user input
   virtual bool              InitTrailingParameters(CExpert_Tparams &parameters);
   
  };

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CTrailing_A_Type : public CExpertModelTrailing
  {
public:
   double            GetTrailingActivate()
     {
      Print("I don't want to use Trailing today");
      return 0;
     }
  };

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CTrailing_B_Type : public CExpertModelTrailing
  {
public:
   double            GetTrailingActivate()
     {
      Print("I want to use Trailing today because it's fun!");
      return 0;
     }
  };

////+------------------------------------------------------------------+
class CExpertModel : public CExpert
  {
public:

   //this is the workaround I could find but it doesn't satisfy me nor answer the question
   CExpertModelTrailing    *m_trail;

   // [...] some other code

   bool              InitTrailing(CExpert_Tparams &parameters)
     {
      int trailingBehaviour = parameters.m_TrailingBehaviour;
      switch(trailingBehaviour)
        {
         case 0 :
            m_trailing = new CTrailing_A_Type();
            // I would like to be able to use m_trailing = new CTrailingNone();
            break;
         case 1 :
         default:
            m_trailing = new CTrailing_B_Type();
            break;
        }

      m_trail = m_trailing;

      double foo = m_trail.GetTrailingActivate();
      // IS WORKING.
      // this will print "I don't want to use Trailing today" if trailingBehaviour == 0
      // this will print "I want to use Trailing today because it's fun!" if trailingBehaviour == 1

      // BUT
      double foo2 = m_trailing.GetTrailingActivate(); 
     //won't work because m_trailing is of CExpertTrailing type.

      return true;
     }
  };

I guess for now I'll just do like this 

m_trail = m_trailing;

Because I can't see how alse I could do...