c++ - How to avoid returning pointers in a class -


assume have class has 3 methods. first methods assigns values first array , rest of methods in order modify computed previous method. since wanted avoid designing methods return array (pointer local variable) picked 3 data member , store intermediate result in each of them. please note simple code used illustration.

class { public: // how class members should accessed isn't important  int * a, *b, *c;  a(int size) {     = new int [size];     b = new int [size];     c = new int [size]; }  void func_a() {      int j = 1;      int(i = 0; < size; i++)         a[i] = j++;   // assign different values }  void func_b() {     int k = 6;      (int = 0; < size; i++)         b[i] = a[i] * (k++); }  void func_c() {     int p = 6;      int (i = 0; < size; i++)         c[i] = b[i] * (p++); } }; 

clearly, if have more methods have have more data members.

** i'd know how can re-design class (having methods return values and) @ same time, class not have of 2 issues (returning pointers , have many data member store intermediate values)

i'm assuming want able modify single array no class-level attributes , without returning pointers. above code can modified single function, i've kept 3 more closely match code.

void func_a(int[] arr, int size){     for(int = 0; < size; i++)         arr[i] = i+1; }  void func_b(int[] arr, int size){     int k = 6;     for(int = 0; < size; i++)         arr[i] *= (k+i); }  //this function func_b unnecessary void func_c(int[] arr, int size){     int p = 6;     for(int = 0; < size; i++)         arr[i] *= (p+i); } 

but if want single function:

void func(int[] arr, int size){     int j = 6;     for(int = 0; < size; i++)         arr[i] = (i+1) * (j+i) * (j+i); } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -