![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Of course:
I realized that the problem is how tocorrectly fill the array shapes[10] with instances of classes derived from CShape. My suggested variant does not work for some reason. Let's think.
The array must be of class pointer type, not of class type itself.
Then we can apply new operator.
The array must be of the class pointers type, not of the class itself.
Then you can apply the new operator.
Can I give you an example?
It's just that the example I gave was taken from documentation and it's not clear how it should work...
I realized that the problem is how tocorrectly fill array shapes[10] with instances of classes derived from CShape. My suggested variant doesn't work for some reason. Let's think.
Think or not, 100 roubles is not money.
The example is 100-percent glitchy, and such an implementation is either not well thought out or not finished.
I won't cite the results of my own experiments in code, I will only give you the conclusions:
1. If you declare objects not as classes but as structures, they will easily fit into an array. But the viriality will have to be removed, and you can only work with the functionality of the type of objects that the array has (in this case, a base class);
2. 2. You can declare them as pointers, and then you can form an array:
a) if pointers are not cleared, memory leaks occur;
b) you also can't work normally with descendant functionality.
PS
To be more exact - functionality which was declared in base class is available and also visible in descendants, but everything that appeared in descendants is inaccessible.
An array must be of type pointers to classes, not of type of class itself.
Then you can apply the new operator.
Yes, I also thought in this direction, about using pointers. But then the conditions from the documentation are not fulfilled:
"For a square (class CSquare) the area is calculated through the sides, for a circle (class CCircle) the area is expressed through the radius and so on. We can create an array to store objects of type CShape, in which we can store both the object of a base class and all its descendants. Later we can call the same function for any element of this array.
...Since we need the corresponding values of m_radius and m_square_side members to calculate the area of a square and a circle, we have added the SetRadius and SetSide() functions to the declaration of the corresponding class. Now we can declare an array of type Shape and fill it with objects of derived classes..."
An array must be of type pointers to classes, not of type of class itself.
Then you can apply the new operator.
Yedelkin:
...Since calculating the area of a square and a circle requires corresponding values of m_radius and m_square_side members, we have added SetRadius and SetSide() functions in the declaration of the corresponding class. Now we can declare an array of Shape type and fill it with objects of derived classes..."
The whole thing is that if the array is of base class type, the descendant functionality cannot be accessed. i.e. SetRadius() and SetSide() become unavailable.
That's why I can see three options:
1. the array should be homogeneous (if possible);
2. I don't understand something about OOP philosophy and architecture of MQL5;
3. Either the developers failed to implement the example, or they didn't follow through with the question.
It won't work properly. It will form an array, though.
To be more exact - functionality which was declared in base class is available, and at the same time it is visible at descendants, but everything that appeared at descendants is inaccessible.
Yup, that's exactly what I came across.
The example is 100 per cent glitchy ...
It turns out that we need an answer from the Handbook authors on this topic.
It won't work properly. It will form an array, though.
The trick is that if the array is of base class type, the descendant functionality cannot be accessed. i.e. SetRadius() and SetSide() become unavailable.
That's why I can see three options:
1. the array should be homogeneous (if possible);
2. I don't understand something about OOP philosophy and architecture of MQL5;
3. the developers have either betrayed an example that doesn't work, or they haven't completed the task.
Execute:
it could be done in a much simpler way... by virtualization:
What do you mean it won't work normally?
1. The descendant functionality is not available. I don't know who or how, but personally I never got access to SetRadius() and SetSide() from the Array.Maybe there's a way to solve the problem with autogenous, but I want to do without it.
2. Maybe I'm working with pointers wrong, but I either have a leak all the time or have to hit the pointers right in the block where the main work is done.