Return Object Pointer

 

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.

class COrder
{
   public:  
      int ticket;
      int type;
      int magic;
      double lots;
      double openPrice;
      double closePrice;
      datetime openTime;
      datetime closeTime;
      double profit;
      double swap;
      double commission;
      double stopLoss;
      double takeProfit;
      datetime expiration;
      string comment;
      string symbol;
};

Then I have a class named "COrderContainer" where all orders are stored. (Deleted the not importent functionst for the forum)

class COrderContainer
{
   private:                            //Member variables     
      COrder*  m_orders[];             // array of order objects
      int      m_index;                // index of current order
      
   public:  
       COrderContainer(void);          //Constructor
      ~COrderContainer(void);          //Destructor     

      
      COrder*  Get(int _index);                 // returns order object with index
      COrder*  Current(void);                   // returns the current order object of container (used by next and prev)      
      COrder*  Prev(void);                      // returns the previous order object of container
      COrder*  Next(void);                      // returns the next order object of container
      COrder*  First(void);                     // returns the first order object of container

};

So the idea is, in my programm I can get f.e. the Symbol of an order:

orderContainer_New.FillOpenOrders();         	//Fill up "new" container
Print(orderContainer_New.Get(0).symbol);   	//Get the symbol of the first 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:

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(NULL);
   }
   return(m_orders[_index]);
} 

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: 

if(CheckPointer(orderContainer_New.Get(-1)) != POINTER_INVALID)
{
   Print(orderContainer_New.Get(-1).symbol);    //Get the symbol of the first order
}

But I'm searching for a solution inside the class function

 
Lukas Roth:

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. 

 

Okey thank you :)


Or would it make sense to build the whole class different so you don't need to check the pointer in the program?

 
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());
Reason: