There were so many things that didn't sit right, so I spent a few mins putting together a more natural version.
#property strict //#include <Object.mqh> class TestCL { protected: int m_id; double m_a[]; public: TestCL(int id, int sz); }; TestCL::TestCL(int id, int sz) : m_id(id) { ArrayResize(m_a, sz); }; class TestDerv1 : public TestCL { private: string m_name; double m_coeff1; double m_coeff2; int m_int0; public: TestDerv1(int id, int sz) : TestCL(id,sz) {}; TestDerv1(int id, int sz, string name, double c1, double c2, int i0); void setParas2(string name, double c1, double c2, int i0); }; // construction and setup in one step TestDerv1::TestDerv1(int id, int sz, string name, double c1, double c2, int i0) : TestCL(id, fmax(sz, i0)), m_name(name), m_coeff1(c1), m_coeff2(c2), m_int0(i0) { } // construction an setup in 2 steps void TestDerv1::setParas2(string name, double c1, double c2, int i0) { if (ArraySize(m_a) > i0) { ArrayResize(m_a, i0); } m_name = name; m_coeff1 = c1; m_coeff2 = c2; m_int0 = i0; } // Script void OnStart() { TestCL* cl1 = new TestDerv1(1, 12, "Test 1", 3.1416, 2.718281828459, 99); TestDerv1* cl2 = new TestDerv1(2, 12); cl2.setParas2("Test 2", 3.1416, 2.718281828459, 99); }
There were so many things that didn't sit right, so I spent a few mins putting together a more natural version.
TestDerv1(int id, int sz, string name, double c1, double c2, int i0);
means that I can't/don't use the constructor of the base class it is overriding the construction method - ok that works!
But I want to use the later may be a lot more complex constructor of the base class (which I do not want to re-write or copy ' paste) in combination with the individual part of the derived class.
I can write as well a simple non-OOP function and within it creates a new instance and than it sets its individual parameter - but that is not OOP.
Thank you. But your solution of
means that I can't/don't use the constructor of the base class it is overriding the construction method - ok that works!
But I want to use the later may be a lot more complex constructor of the base class (which I do not want to re-write or copy ' paste) in combination with the individual part of the derived class.
I can write as well a simple non-OOP function and within it creates a new instance and than it sets its individual parameter - but that is not OOP.
TestDerv1::TestDerv1(int id, int sz, string name, double c1, double c2, int i0) : TestCL(id, fmax(sz, i0)), // <<< This is calling the Constructor of the base class m_name(name), m_coeff1(c1), m_coeff2(c2), m_int0(i0) { }
The solution above does call the constructor of the base class. If you wish to make the base class constructor more complex you just expand the derived class constructor args and pass them through
TestDerv1::TestDerv1(int id, int sz, int extra1, double extra2, double extra3, string name, double c1, double c2, int i0) : TestCL(id, fmax(sz, i0), extra1, extra2, extra3), // Extra args passed through to more complex base constructor m_name(name), m_coeff1(c1), m_coeff2(c2), m_int0(i0) { }
If this is not what you mean then sorry I really don't understand what you are trying to achieve.
My problem is systematically!
Theses Parameters ( int id, int sz ) are for all derived classes the same and should be treated by the constructor of the base method. Just assume that theses are not the only one and it will do a lot more than setting an object and sizing an array - this is just an example. Therefore I want to define the constructor of of the derived class TestDerv1 by the use of the base class TestCL:
TestDerv1(int id, int sz) : TestCL(id,sz) {};
The second method with (string name, double c1, double c2, int i0 ) is individual for any derived class. It has to be written for any of them.
One solution to solve the problem is to write for all individual derived classes their individual constructor or as I said to write a non-OOP function that does the trick.
But I would like to know whether and how I can realize my OOP idea - or why it is not possible. It can be that C++ might be able to do the trick but MQL4/5 can't.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
My aim:
One base class with its constructor and several different derived classes with different parameters using the one constructor of the base class.
The default approach would be a
This is realized by class TestDerv1 the constructor and the method setParas2(..).
But now I'd like to do both: the constructor and the method setParas2(..) in one method setParas1(..) which returns the reference of the constructed instance and which would enable me to call only 1 method!!
This is realized in the attached script - but with two compiler errors that I'd like to get rid off - if I would know how!!Unfortunately this causes two compiler errors:
So how do I call in line 63 a method of a class that is not yet created?
Why do I get in line 65 the function not defined message?
Thanks in advance!!