OOPortal 





Building C++ Classes  «Prev  Next»
External member functions

Explicit inline Functions

The inline specification can be used explicitly with member functions defined at file scope.
This avoids having to clutter the class definition with function bodies.
struct ch_stack {
   .....
   void reset();
   void push(char c);
   .....
};

inline void ch_stack::reset()
{
   top = EMPTY;
}

inline void ch_stack::push(char c)
{
   assert(top != FULL);
   top++;
   s[top] = c;
}


Explicit Inlining
In explicit inlining, the prototype in the class definition is preceded by the keyword inline.
The member function defintion is defined outside the class definition, and is also preceded by the keyword inline.
Example:
class Date{
public:
  inline Date(int,int,int);
  inline void setdate(int,int,int);
  void showdate();  // not inlined
  // additional member functions
private:
  int month;
  int day;
  int year;
};

inline Date::Date(int mm,int dd,int yy){
  month = mm;
  day = dd;
  year = yy;
}

inline void Date::setdate(int mm,int dd,int yy){
  month = mm;
  day = dd;
  year = yy;
}

void showdate(){
  cout << month << '/' << day << '/' << year;
}