Programming Tutorials

Tutorial on Inline Implementation in C++

By: Jagan in C++ Tutorials on 2007-09-09  

Just as you can ask the compiler to make a regular function inline, you can make class methods inline. The keyword inline appears before the return value. The inline implementation of the GetWeight() function, for example, looks like this:

inline int Cat::GetWeight()
return itsWeight;         // return the Weight data member

You can also put the definition of a function into the declaration of the class, which automatically makes that function inline. For example,

class Cat
int GetWeight() { return itsWeight; }    // inline
void SetWeight(int aWeight);

Note the syntax of the GetWeight() definition. The body of the inline function begins im-mediately after the declaration of the class method; there is no semicolon after the paren-theses. Like any function, the definition begins with an opening brace and ends with a closing brace. As usual, whitespace doesn't matter; you could have written the declaration as

class Cat
int GetWeight()
return itsWeight;
}                           // inline
void SetWeight(int aWeight);

The following two programs re-create the Cat class, but they put the declaration in CAT.HPP and the implementation of the functions in CAT.CPP. The second program below also changes the accessor functions and the Meow() function to inline.

Cat class declaration in CAT.HPP

1:  #include <iostream.h>
2:  class Cat
3:   {
4:  public:
5:   Cat (int initialAge);
6:   ~Cat();
7:    int GetAge() { return itsAge;}             // inline!
8:    void SetAge (int age) { itsAge = age;}     // inline!
9:    void Meow() { cout << "Meow.\n";}             // inline!
10:  private:
11: int itsAge;
12: };

Cat implementation in CAT.CPP.

1:   // Demonstrates inline functions
2:   // and inclusion of header files
4:   #include "cat.hpp"  // be sure to include the header files!
7:   Cat::Cat(int initialAge)   //constructor
8:   {
9:      itsAge = initialAge;
10:  }
12:  Cat::~Cat()             //destructor, takes no action
13:  {
14:  }
16:  // Create a cat, set its age, have it
17:  // meow, tell us its age, then meow again.
18:  int main()
19:  {
20:     Cat Frisky(5);
21:     Frisky.Meow();
22:     cout << "Frisky is a cat who is " ;
23:     cout << Frisky.GetAge() << " years old.\n";
24:     Frisky.Meow();
25:     Frisky.SetAge(7);
26:     cout << "Now Frisky is " ;
27:     cout << Frisky.GetAge() << " years old.\n";
28:      return 0;
29: }

Output: Meow.
Frisky is a cat who is 5 years old.
Now Frisky is 7 years old.

Analysis: Three of the methods are written inline in the declaration file and the declaration has been separated into CAT.HPP.
GetAge() is declared in line 7, and its inline implementation is provided. Lines 8 and 9 provide more inline functions, but the functionality of these functions is unchanged from the previous "outline" implementations.

Line 4 shows #include "cat.hpp", which brings in the listings from CAT.HPP. By including cat.hpp, you have told the precompiler to read cat.hpp into the file as if it had been typed there, starting on line 5.

This technique allows you to put your declarations into a different file from your implementation, yet have that declaration available when the compiler needs it. This is a very common technique in C++ programming. Typically, class declarations are in an .HPP file that is then #included into the associated CPP file.

Lines 18-29 making these functions inline doesn't change their performance.

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in C++ )

Latest Articles (in C++)