Static Class member- Member functions u Suppose we need to implement a member function is_ elemo to check whether an element is in the Fibonacci sequence It will be only related to the s_elems vector a If a member function never access any non static data members it is then independent with any object instances, We could declare it as a static member function
Static Class Member – Member Functions ◼ Suppose we need to implement a member function is_elem() to check whether an element is in the Fibonacci sequence ◼ It will be only related to the s_elems vector ◼ If a member function never access any nonstatic data members, it is then independent with any object instances, we could declare it as a static member function
Static Class Member-Member Function (cont, class FIbonacci public: static bool is elem(int); /declaration bool CFibonacci: is elem(int iValue) /definition, class scope is needed but“ static is not if (!s_elems size()ll(s_elems[s_elems size(-1]< iValue)) gen_elems to value(iValue); /another static member funtion vector<int> iterator found it, end it=s_ elems endo; found_it= find(s_elems begin(, end_it, iValue); return found it l= end it
Static Class Member – Member Function (cont.) class CFibonacci { public: static bool is_elem(int); //declaration //… }; bool CFibonacci::is_elem(int iValue) //definition, class scope is needed, //but “static” is not { if ((! s_elems.size()) || (s_elems[s_elems.size()-1] < iValue)) gen_elems_to_value(iValue); //another static member funtion vector<int>::iterator found_it, end_it = s_elems.end(); found_it = find(s_elems.begin(), end_it, iValue); return found_it != end_it; }
Static Class Member-Member Function (cont, u Benefit- We could directly call these functions without instantiating any objects bool flsElem CFibonacci: is elem(ival) # nclude“ Fibonacci” cout < iVal ( fleEm?“is”:" is not") int main( <" a element of the sequence. ≤<endl<≤“ Try another?(YN)”; char ch. bool continue true: cin > ch. int ival: if(N=ch n==ch cOntinue false. while(cOntinue) cout <<Enter Value: " Remember: only member functions that cin>>ⅳva never access non-static data members could be declared static
Static Class Member – Member Function (cont.) ◼ Benefit – We could directly call these functions, without instantiating any objects //… #include “Fibonacci.h” int main() { char ch; bool fContinue = true; int iVal; while (fContinue) { cout << “Enter Value: ”; cin >> iVal; bool fIsElem = CFibonacci::is_elem(iVal); cout << iVal << (fIsElem ? “ is ” : “ is not”) << “a element of the sequence.” << endl << “Try another? (Y/N)”; cin >> ch; if (‘N’==ch || ‘n’==ch) fContinue = false; } } ◼ Remember: only member functions that never access non-static data members could be declared static
Const and mutable
Const and Mutable
Const member function u If a class is declared as a parameter of const reference in a function could we use all its member functions? int sum(const CFibonacci&);//may change m_iNext As it declared as const, we can invoke only const member functions which telling the compiler they won't modify the object (may be compiler-dependent)
Const member function ◼ If a class is declared as a parameter of const reference in a function, could we use all its member functions? int sum(const CFibonacci&); //may change m_iNext ◼ As it declared as const, we can invoke only const member functions which telling the compiler they won’t modify the object (may be compiler-dependent)