Inline's and < Static's
‘Inline’s and ‘Static’s
Inline member functions u Implicit inline member function Member functions defined in the class body, automatically regarded as inline functions Eg, STack; size( a,class scope resolution operator Explicit inline member function Member functions if defined outside the class body inline should be manually specified inline bool CStack: emptyoi return m_ stack. empty o:y a Definitions of both types inline functions should be placed in the head file, for better visibility
Inline member functions ◼ Implicit inline member function ◼ Member functions defined in the class body, automatically regarded as inline functions ◼ Eg. CStack::size() ◼ :: – class scope resolution operator ◼ Explicit inline member function ◼ Member functions if defined outside the class body, ‘inline’ should be manually specified ◼ Eg. inline bool CStack::empty() { return m_stack.empty(); } ◼ Definitions of both types inline functions should be placed in the head file, for better visibility
Static Class Member -Data Members u Another example: the fibonacci number sequence class The sequence is fixed, so every object of the class maintain a copy is needless Every instance uses only a range of elements in the sequence, related status information are Beginning position -start point in the original sequence ength - range size of current object Next Element- current position How to define the class?
Static Class Member – Data Members ◼ Another example: the fibonacci number sequence class ◼ The sequence is fixed, so every object of the class maintain a copy is needless ◼ Every instance uses only a range of elements in the sequence, related status information are: Beginning position – start point in the original sequence Length – range size of current object Next Element – current position ◼ How to define the class?
Static Class Member -Data Members (cont, class FIbonacci public l/we define interface later rivate. int m iBegPos int m_ iLength int m iNext: static vectors<int> s elems l/declaration FIbonacci s elems fib fib fib2 m iBeginpos m_iBeginPos m_iLength m_ibeginPOS m_iLength INext L gt th m iNext
Static Class Member – Data Members (cont.) class CFibonacci { public: //we define interface later private: int m_iBegPos; int m_iLength; int m_iNext; static vector<int> s_elems; //declaration }; CFibonacci::s_elems m_iBeginPos m_iLength m_iNext m_iBeginPos m_iLength m_iNext m_iBeginPos m_iLength m_iNext fib1 fib2 fib3
Static Class Member -Data Members (cont, Static data members Only one entity among all objects of this class All objects share the static data members and access them Should be defined and initialized outside the cla vector<int> CFibonacci: s elems definition and intialized as an empty vector, like global variables Iclass scope is needed, but keyword"static"is no more needed int Fibonacci s iDefaultPos =8: / definition and initialized Const objects could be done in the class [other pal ate: static const int s_iBuffSize = 1024; //Compiler dependent int m Buffer[s iBuffSize]
Static Class Member – Data Members (cont.) ◼ Static data members ◼ Only one entity among all objects of this class ◼ All objects share the static data members and access them ◼ Should be defined and initialized outside the class vector<int> CFibonacci::s_elems; //definition and intialized as an empty vector, like global variables //class scope is needed, but keyword ”static” is no more needed int Cfibonacci::s_iDefaultPos = 8; //definition and initialized ◼ Const objects could be done in the class { //Other parts private: static const int s_iBuffSize = 1024; //Compiler dependent int m_Buffer[s_iBuffSize]; }