Hello Community.
I'm creating a new class for working with orders.
I haven't worked a lot with pointers.
There is a Class "COrder", it contains all the information about the order.
Then I have a class named "COrderContainer" where all orders are stored. (Deleted the not importent functionst for the forum)
So the idea is, in my programm I can get f.e. the Symbol of an order:
But, the problem is if I have 0 orders or i use a index wich is not in the range of the array I get an Error...
Code of the function Get:
My Idea was, if the index is out of range I return NULL
But this still creates an error in the main programm.
How would you guys solve this? :)
So you don't missunderstand me, I know it could be solved in program like that:
But I'm searching for a solution inside the class function
You can return an empty Object:
COrder* COrderContainer::Get(int _index) { if(_index < 0 || _index >= ArraySize(m_orders)) { Print("Index out of Range! Index: ",_index, ", Container Size: ", ArraySize(m_orders)); return m_empty_order ; //Create an empty order in the constructor of the container and delete in the destructor } return(m_orders[_index]); }
but you will still need to check the symbol whether it is empty or not.
So your solution, checking for invalid pointer makes more sense.
bool COrderContainer::Get(int _index,COrder* &_order) { if(_index < 0 || _index >= ArraySize(m_orders)) { Print("Index out of Range! Index: ",_index, ", Container Size: ", ArraySize(m_orders)); return false; } _order=m_orders[_index]; return true; }
COrder *order; if(orderContainer_New.Get(idx,order)) Print(idx,"->",order.Symbol());

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello Community.
I'm creating a new class for working with orders.
I haven't worked a lot with pointers.
There is a Class "COrder", it contains all the information about the order.
Then I have a class named "COrderContainer" where all orders are stored. (Deleted the not importent functionst for the forum)
So the idea is, in my programm I can get f.e. the Symbol of an order:
But, the problem is if I have 0 orders or i use a index wich is not in the range of the array I get an Error...
Code of the function Get:
My Idea was, if the index is out of range I return NULL
But this still creates an error in the main programm.
How would you guys solve this? :)
So you don't missunderstand me, I know it could be solved in program like that:
But I'm searching for a solution inside the class function