c++ - Storing intermediate results in a class's attribute -


assume have class containing 3 methods , attribute. class supposed encode values in array called result. 3 functions run in order last encoded result stored in result.

class a{     public:          int size;         int* result;          a(int si){             size=si;             result=new int[size];             for(int i=0;i<size;i++)                     result[i]=5;         }          void func_1(){             for(int i=0;i<size;i++)                 result[i]=i+1;         }         void func_2(){             for(int j=0;j<size;j++)                 result[j]=j+10;         }         void func_3(){             for(int k=0;k<size;k++)                 result[k]=k+4;         } };  int main(){     a(10);     a.func_1(); // consider each method encoding function (e.g. encryption, randomization, etc)     a.func_2();     a.func_3();     return 0; } 

here stored intermediate results in data member array called result.

my question whether storing intermediate results in class's attribute , keep updating bad idea? (i.e. violates class definition)

if isn't idea alternative be?

my question whether storing intermediate results in class's attribute , keep updating bad idea?

as general guideline, object should not left in invalid state after completion of function. constitutes valid state depends on object , application.

having said that, have seen many instances object in invalid state after completion of call. 1 example can give comes domain knowledge. finite element mesh has in state considered valid mesh. however, whether constructing mesh reading data file or building in memory geometry using sort of meshing algorithm, there lot of function calls before mesh object defined , in valid state. in mean time, in invalid state while.

follow guideline give slack.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -