Accessing child function from base class - page 2

Dominik Christian Egert
2606
Samuel Manoel De Souza #:

Here is the example. and the output.


Clear example.
Yes, you can call the function from within the object like you have shown.

But you cannot call child::update from within.

You can only call known functions. At least that's what I meant.



Samuel Manoel De Souza
11245

Actually can do something like that.
He don't need know the concrete object since it has the update() method. so can use some other base class, or even the CBase if updade() is virtual, if not virtual need derive childs from another class/interface which has the update() method.

interface IChild
  {
   void           update();
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Base
  {
   IChild*           m_child;
public:
                     Base(IChild* pointer = NULL): m_child(pointer) {;}
                    ~Base(void) {;}

   void              update(void)
                     {if(CheckPointer(m_child) != POINTER_INVALID) m_child.update();}
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Child1: public IChild
  {
public:
                     Child1(void) {;}
                    ~Child1(void) {;}

   void              update() {Print(__FUNCTION__);}
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Child2: public IChild
  {
public:
                     Child2(void) {;}
                    ~Child2(void) {;}

   void              update() {Print(__FUNCTION__);}
  };
Samuel Manoel De Souza
11245

In the initial question childs was derived from CBase

class CChild : protected CBase

So he can do that also. and don't need the interface

class Base
  {
   Base*             m_child;
public:
                     Base(Base* pointer = NULL): m_child(pointer) {;}
                    ~Base(void) {;}

   virtual void      update(void)
                     {if(CheckPointer(m_child) != POINTER_INVALID) m_child.update();}
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Child1: public Base
  {
public:
                     Child1(void) {;}
                    ~Child1(void) {;}

   virtual void      update() {Print(__FUNCTION__);}
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Child2: public Base
  {
public:
                     Child2(void) {;}
                    ~Child2(void) {;}

   virtual void      update() {Print(__FUNCTION__);}
  };
Dominik Christian Egert
2606
Yes, now you have coded where I wanted him to go.


William Roeder
28234
William Roeder  
Dominik Christian Egert #: Yes, now you have coded where I wanted him to go.

Unnecessary. Update() is virtual; just call it.

Dominik Christian Egert
2606
William Roeder #:

Unnecessary. Update() is virtual; just call it.

Would it be possible to declare it fully virtual and still create a pointer of base class type?


class Base
  {
public:
                     Base() {};
                    ~Base(void) {};

   virtual void      update(void) = 0;
  };
 



William Roeder
28234
William Roeder  
Dominik Christian Egert #: Would it be possible to declare it fully virtual and still create a pointer of base class type?
One has nothing to do with the other.
Base* obj = (Base*)new Class2();
   obj.update();
delete obj;
Dominik Christian Egert
2606
William Roeder #:
One has nothing to do with the other.
Thank you.

Mohd Syafiq Johar Arrifin
337
R4tna C #:

That is a surprise as I use this approach a lot and have not had such issues. Were you by nay chance creating a new object on every tick?

That could consume a lot of memory - I am sure you know already, but it is better to instantiate once and re-use.

Also, what is it you are planning to do with this approach? Seems very involved and intricate

That is a surprise as I use this approach a lot and have not had such issues. Were you by nay chance creating a new object on every tick?

- The object were create once on child. But multiple child. But that's for now. Since this library meant to be used on EA, later on will be execute on every tick. I'm aware of that. Array also does much impact on memory


Also, what is it you are planning to do with this approach? Seems very involved and intricate

- My EA, scripts, indicator share the same library. Since it's the same library, previously all the code is store in include\main.mqh. And has thousands line of code in it. But it give me headache to find what I need. So I decide to refactor/migrate all code into separate files. The class still the same. Some code are easy to handle because there's no dependencies. But there are a few class with thousand of line of codes that I want to separate into multiple files small class.

After doing that. Since a big class is split into multiple small class.

Every times the base update is called to be updated, the derived class is update as well.


Since there's a data that required on derived class from protected base.

So every time data on protected base is update, all the child class need to update as well.


Samuel Manoel De Souza #:

Here is the example. and the output.


Hi Samuel Manoel De Souza . Thank you.. The proposed method work..

At first I encounter the class is inaccessible. But here explained why. Because of I'm extending protected. After changing it to public all back on track.


After refactor all the code, and compiled without any error. But I had stack overflow error when the script is applied to the chart.

Anyone know how to see the log of this runtime error?

conversion from derived * to base * exists but is inaccessible
conversion from derived * to base * exists but is inaccessible
  • 2012.05.06
  • user1232138 user1232138 5,311 6 6 gold badges 35 35 silver badges 61 61 bronze badges
  • stackoverflow.com
Why does the follwing code produce this error even though c is a struct and has a public inheritance by default??