"stackable" methods

 

This is a noob question.

I wont run into any walls if i deploy methods this way will i ?

(per the convenience of being able to call these methods without using the returned pointer , like they were void type)

class thing_with_stackable_methods{
string                        name;
int                           volume;
                              public:
                              thing_with_stackable_methods(void){reset();}
                             ~thing_with_stackable_methods(void){reset();}
thing_with_stackable_methods* reset(){
                              name=NULL;
                              volume=0;
                              return(GetPointer(this));
                              }
thing_with_stackable_methods* set_name(string _new_name){
                              name=_new_name;
                              return(GetPointer(this));
                              }
thing_with_stackable_methods* set_volume(int new_volume){
                              volume=new_volume;
                              return(GetPointer(this));
                              }
                       string get_name(){return(name);}
                          int get_volume(){return(volume);}                              
};
int OnInit()
  {
//---
  thing_with_stackable_methods a_thing;
  
  a_thing.set_name("EURUSD").set_volume(55);//not using the pointer returned
  a_thing.set_name("GBPUSD");//same
  
  
  delete(GetPointer(a_thing));
//---
   return(INIT_SUCCEEDED);
  }

Tx

 
a_thing.set_name("EURUSD").set_volume(55);//not using the pointer returned
You are using the returned pointer. Same as a_thing.set_volume(…);
 

i wonder what the "stack" limit is  😄

#define F set_volume(1)

int OnInit()
  {
//---
  thing_with_stackable_methods a_thing;
  
  a_thing.set_name("EURUSD").set_volume(55);
  a_thing.set_name("GBPUSD");
  a_thing.set_name("EURUSD").set_volume(11).reset().set_name("BTC").set_volume(101);
  Print(a_thing.get_name()+" "+IntegerToString(a_thing.get_volume()));
  
  a_thing.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F.F;
  
  delete(GetPointer(a_thing));
//---
   return(INIT_SUCCEEDED);
  }
 
  delete(GetPointer(a_thing));
This is wrong. You did not allocate a_thing with new, you do not de-allocate it with delete.
 

I've been doing this in structures containing class arrays and classes containing class arrays in their constructor / destructor too . It could be redundant as well 

class item{
int value;
};

class item_holder{
item my_items[];
item_holder(void){reset();}
~item_holder(void){reset();}
void reset(){
for(int i=0;i<ArraySize(my_items);i++){
delete(GetPointer(my_items[i]));
}       
ArrayFree(my_items);
}
};
 
Lorentzos Roussos #: I've been doing this in structures containing class arrays and classes containing class arrays in their constructor / destructor too . It could be redundant as well 
item my_items[];
⋮
delete(GetPointer(my_items[i]));

Again, this is wrong. my_items[] contains item's, not pointers to them. You did not allocate them with new, you do not delete them.

 

hmm there seems to be a speed gain too without my mistake as the array grows in size , nice . I must re-study some things evidently

class an_item{
double value;
       public:
       an_item(void){value=0.0;}
  void set(double _v){value=_v;}
};

class items_with_delete{
an_item items[];
        public:
        items_with_delete(void){reset();}
       ~items_with_delete(void){reset();}
   void reset(){
        for(int i=0;i<ArraySize(items);i++){delete(GetPointer(items[i]));}
        ArrayFree(items);
        }
   void add_from_array(double &array[],int times_to_repeat){
        for(int i=0;i<times_to_repeat;i++){
        reset();
        ArrayResize(items,ArraySize(array),0);
        for(int j=0;j<ArraySize(array);j++){items[j].set(array[j]);}
        }
        }
};

class items_no_delete{
an_item items[];
        public:
        items_no_delete(void){reset();}
       ~items_no_delete(void){reset();}
   void reset(){
        ArrayFree(items);
        } 
   void add_from_array(double &array[],int times_to_repeat){
        for(int i=0;i<times_to_repeat;i++){
        reset();
        ArrayResize(items,ArraySize(array),0);
        for(int j=0;j<ArraySize(array);j++){items[j].set(array[j]);}
        }
        }             
};


int OnInit()
  {
//---
  EventSetTimer(1);  
//---
   return(INIT_SUCCEEDED);
  }

void OnTimer(){
  Print("Starting");
  EventKillTimer();
  int array_items=100000,rounds=1000;
  double array[];
  ArrayResize(array,array_items,0);
  for(int i=0;i<array_items;i++){array[i]=i;}
  //with delete
  uint starttime=GetTickCount();
  items_with_delete iwd;
  iwd.add_from_array(array,rounds);
  uint endtime=GetTickCount();
  long duration=endtime-starttime;
  if(endtime<starttime){duration=(endtime+UINT_MAX)-starttime;}
  Print("Duration with delete "+IntegerToString(duration)+"ms");
  //without delete
  starttime=GetTickCount();
  items_no_delete ind;
  ind.add_from_array(array,rounds);
  endtime=GetTickCount();
  duration=endtime-starttime;
  if(endtime<starttime){duration=(endtime+UINT_MAX)-starttime;}
  Print("Duration without delete "+IntegerToString(duration)+"ms");  
}

Reason: