3.22 Inline Methods


Do not remove methods for the sake of efficiency - use inline methods instead. For example:

  class Account
  {
    protected:	
      int accountNumber;
    public:
      // an example inline method
      int getAccountNumber() { return accountNumber; }
  };

This is better than making the accountNumber state public, retaining encapsulation. Inline is ignored with virtual methods. We can provide full access to a state through methods, rather than directly to the state by making it public, by providing an accessor and a mutator. For example for the accountNumber state in the Account class, these would look like

   int getAccountNumber(); //accessor
   void setAccountNumber(int newNumber); //mutator

The main advantage of this format is that access can be controlled to the accountNumber state, so, if additional functionality is required at a later stage, it can be achieved, without effecting the developers using the Account class.

We can also declare standalone methods (global methods) to be inline, such as:

	inline int account::getAccountNumber() { //etc }


These notes are copyright Dr. Derek Molloy, School of Electronic Engineering, Dublin City University, Ireland 2013-present. Please contact him directly before reproducing any of the content in any way.
Comments