Programming own collection class

Back to topics list  | 1 2 To post a new topic, please log in or register
avatar
12985
WHRoeder 2015.02.17 14:14 #
 
Ovo:

This part is a bit weird (inconsistent) from my point of view.

void add(Order& order)
{
.....
  orders[(size - 1)] = GetPointer(order);
}
 
Order* order = new Order();
....
orders.add(order);

 I would code it as

void add(Order* order)
{
.....
  orders[(size - 1)] = order;
}
They both do the same thing with your add(new). But the former allows the call to work with either a pointer or an instance.
Order globalOrder;
:
add(globalOrder);
as opposed to
Order globalOrder;
:
add( GetPointer(globalOrder) ); // Why does the caller need to know
                                // add internally stores a pointer?

I would code it thus:
void add(Order& order){ add( GetPointer(order) ); }
void add(Order* order) ...
        int pointer;
        int size;
       
        void OrderCollection()
        {
            pointer = -1;
            size = 0;
        }
Get in the habit of using initialization lists. Initialize once instead of default initialization and then assign.
        int pointer;
        int size;
       
        void OrderCollection()
                  : pointer(-1),
                    size(0)
                      {/*nothing more needs to be done*/}

avatar
1109
Ovo 2015.02.17 14:49 #
 
WHRoeder:


They both do the same thing with your add(new). But the former allows the call to work with either a pointer or an instance.as opposed to
I would code it thus:

I did not state it did not work. I have little use for collections with static instances, but if you have it different, I have no problem with it.
Back to topics list   | 1 2  

To add comments, please log in or register