A corrected version Local Static Objects u Eems declared as local objects in the function which doesn? t exist after the function returns Although declare it in the file scope will work it is not a good idea A risk, functions are harder to understand (logic about not only the functions but also the global variables needs to understand) A bad design, breaking the independency between the functions and increasing the coupling a We could work it out by using local static objects
A corrected version – Local Static Objects ◼ Elems declared as local objects in the function, which doesn’t exist after the function returns ◼ Although declare it in the file scope will work, it is not a good idea ◼ A risk, functions are harder to understand (logic about not only the functions but also the global variables needs to understand) ◼ A bad design, breaking the independency between the functions and increasing the coupling ◼ We could work it out by using local static objects
A corrected version Local Static Objects(cont const vector <int> fubon seq (int iLengt f(X==0‖ⅸ==1) static vector int Elems Elems push back (1) if (iLength <=0 iLength 1024 else er<< ength”<< iLength < not supported"<< endl; Elems push back(Elems[iX-1 return 0 Elems[iX-2]) l/existing elements won't be calculated return &elems for(intⅸ= Elem. size(;Ⅸ< iLength;ⅸ++)}
A corrected version – Local Static Objects (cont.) const vector<int>* fibon_seq(int iLength) { static vector<int> Elems; if (iLength <= 0 || iLength > 1024) { cerr << “Length ” << iLength << “not supported” << endl; return 0; } //existing elements won’t be calculated for (int iX = Elem.size(); iX < iLength; iX++) { if (iX == 0 || iX == 1) { Elems.push_back(1); } else { Elems.push_back(Elems[iX-1] + Elems[iX-2]); } } return &Elems; }