|

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
{
public:
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
{
public:
int
GetWeight()
{
return
itsWeight;
}
// inline
void
SetWeight(int aWeight);
};
Listings
6.6 and 6.7 re-create the Cat class, but they put
the declaration in CAT.HPP and the implementation
of the functions in CAT.CPP. Listing 6.7 also changes
the accessor functions and the Meow() function to
inline.
Listing
6.6. 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:
};
Listing
6.7. Cat implementation in CAT.CPP.
1:
// Demonstrates inline functions
2:
// and inclusion of header files
3:
4:
#include "cat.hpp" // be sure to include the header
files!
5:
6:
7:
Cat::Cat(int initialAge) //constructor
8:
{
9:
itsAge = initialAge;
10:
}
11:
12:
Cat::~Cat() //destructor, takes no action
13:
{
14:
}
15:
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.
Meow.
Now Frisky is 7 years old.
Analysis: The code presented in Listing
6.6 and Listing 6.7 is similar to the code in Listing
6.4, except that 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 of Listing 6.7 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 repeat the main function from Listing 6.4.
This shows that making these functions inline doesn’t
change their performance.
|